public static bool IsDate(object lString)
{
bool bolReturnValue = false;
string sDate = lString.ToString();
int[] aMonth, aLeapMonth;
string[] aDate;
int iDay, iMonth, iYear;
char[] aDelimiter = new char[]{Convert.ToChar("/"), Convert.ToChar("-")};
aMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aLeapMonth = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aDate = sDate.Split(aDelimiter);
if (aDate.Length!=3)
return false;
if (!IsNumeric(aDate[0].ToString()))
return false;
if (!IsNumeric(aDate[1].ToString()))
return false;
if (!IsNumeric(aDate[2].ToString()))
return false; iYear = (int)aDate[0];
iMonth = (int)aDate[1];
iDay = (int)aDate[2];lString的值是"2006-7-15"
在aDate[0]~aDate[2]的值赋给iYear,iMonth,iDay时候,既然是错乱的,值分别是:
iYear = 15
iMonth = 2006
iDay = 7奇怪不?
{
bool bolReturnValue = false;
string sDate = lString.ToString();
int[] aMonth, aLeapMonth;
string[] aDate;
int iDay, iMonth, iYear;
char[] aDelimiter = new char[]{Convert.ToChar("/"), Convert.ToChar("-")};
aMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aLeapMonth = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aDate = sDate.Split(aDelimiter);
if (aDate.Length!=3)
return false;
if (!IsNumeric(aDate[0].ToString()))
return false;
if (!IsNumeric(aDate[1].ToString()))
return false;
if (!IsNumeric(aDate[2].ToString()))
return false; iYear = (int)aDate[0];
iMonth = (int)aDate[1];
iDay = (int)aDate[2];lString的值是"2006-7-15"
在aDate[0]~aDate[2]的值赋给iYear,iMonth,iDay时候,既然是错乱的,值分别是:
iYear = 15
iMonth = 2006
iDay = 7奇怪不?
iYear = dt.Year;
iMonth = dt.Month;
iDay = dt.Day;
你的代码应该编译不过去
iYear = (int)aDate[0];
string不能这样转int如果要判断输入的日期是否有效
可以用比较笨的try catchtry
{
DateTime.Parse(string);
return true;
}
catch
{
return false;
}
或者用正则
也得不到你说的结果
tring sDate = lString.ToString();
string[] aDate;
string iDay, iMonth, iYear;
char[] aDelimiter = new char[]{Convert.ToChar("/"), Convert.ToChar("-")};
aDate = sDate.Split(aDelimiter);iYear = aDate[0];
iMonth = aDate[1];
iDay = aDate[2];
这样得到的结果是正确的
iYear = 15
iMonth = 2006
iDay = 7我要得到值分别是:
iYear = 2006
iMonth = 7
iDay = 15
iMonth = 2006
iDay = 7
DateTime dt = DateTime.Parse(str);
textBox1.Text = dt.Year.ToString(); textBox2.Text = dt.Month.ToString(); textBox3.Text = dt.Day.ToString();
{
try
{
DateTime.Parse(string);
return true;
}
catch
{
return false;
}
}
{
bool bolReturnValue = false;
string sDate = lString.ToString();
int[] aMonth, aLeapMonth;
string[] aDate;
int iDay=0, iMonth=0, iYear=0;
char[] aDelimiter = new char[]{Convert.ToChar("/"), Convert.ToChar("-")};
aMonth = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aLeapMonth = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
aDate = sDate.Split(aDelimiter);
if (aDate.Length!=3)
return false;
if (!IsNumeric(aDate[0].ToString()))
return false;
if (!IsNumeric(aDate[1].ToString()))
return false;
if (!IsNumeric(aDate[2].ToString()))
return false; iYear = Convert.ToInt32(aDate[0]);
iMonth = Convert.ToInt32(aDate[1]);
iDay = Convert.ToInt32(aDate[2]);
if (iMonth<0 || iMonth>12)
return false; if ((iDay != 0) && (iMonth != 0) && (iYear != 0))
{
if ((iYear%4) == 0)
{
if (((iYear%100) == 0) && ((iYear%400) != 0))
{
if (iDay<=aMonth[iMonth - 1])
bolReturnValue = true;
}
else
{
if (iDay<=aLeapMonth[iMonth - 1])
bolReturnValue = true;
}
}
else
{
if (iDay<=aMonth[iMonth - 1])
bolReturnValue = true;
}
}
return bolReturnValue;
}
#endregion
iMonth = Convert.ToInt32(aDate[1]);
iDay = Convert.ToInt32(aDate[2]);并没有得到我想要的结果!!!
object lString ?????
iYear = (int)aDate[0];
iMonth = (int)aDate[1];
iDay = (int)aDate[2];或者是强制类形转换时地址出现混乱。晕。
string eDate = "2006-7-15";
object lString = eDate;
string sDate = lString.ToString();
DateTime dt = DateTime.Parse(sDate);
int iYear = Convert.ToInt16(dt.Year);
int iMonth = Convert.ToInt16(dt.Month);
int iDay = Convert.ToInt16(dt.Day);
//string result = dt.ToString("MM-dd-yyyy");
Response.Write(iYear+"-"+iMonth+"-"+iDay);在aspx文件中是没有什么问题的
string dateString = "20061109";
try
{
date= DateTime.ParseExact(dateString, "yyyyMMdd", null);
}
catch (FormatException ex)
{
return false;
}
return true
目的是不管什么格式日期都转换为欧洲格式(美国日期)
------------------------
什么意思哦?欧洲格式和美国日期不同吧?楼主的意思是不是说,不管过来的是什么样的日期,都转为11.9.2006这种格式?
string dateString = "20061109";
try
{
date= DateTime.ParseExact(dateString, "yyyyMMdd", null);
}
catch (FormatException ex)
{
return false;
}
return true
{
DateTime dt = DateTime.Parse(strDT);
textBox1.Text = dt.ToString("MM/dd/yyyy"); -- 美国格式
}
catch(Exception ex)
{
MessageBox.Show("Format error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}