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奇怪不?

解决方案 »

  1.   

    iDay   = (int)aDate[2];后面代码省略了
      

  2.   

    DateTime dt = DateTime.Parse("2006-7-15");
    iYear = dt.Year;
    iMonth = dt.Month;
    iDay = dt.Day;
      

  3.   

    不知道你要干什么
    你的代码应该编译不过去
    iYear = (int)aDate[0];
    string不能这样转int如果要判断输入的日期是否有效
    可以用比较笨的try catchtry
    {
        DateTime.Parse(string);
        return true;
    }
    catch
    {
        return false;
    }
    或者用正则
      

  4.   

    而且把你的代码改的可以执行
    也得不到你说的结果
    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];
    这样得到的结果是正确的
      

  5.   

    上面代码运行后,值分别是:
    iYear  = 15
    iMonth = 2006
    iDay   = 7我要得到值分别是:
    iYear  = 2006
    iMonth = 7
    iDay   = 15
      

  6.   

    大家给的方法都是,还是得到 iYear  = 15
    iMonth = 2006
    iDay   = 7
      

  7.   

    string str = "2006-02-15";
                DateTime dt = DateTime.Parse(str);
         
                textBox1.Text = dt.Year.ToString();            textBox2.Text = dt.Month.ToString();            textBox3.Text = dt.Day.ToString();
      

  8.   

    public static bool IsDate(object lString)
    {
       try
      {
        DateTime.Parse(string);
        return true;
      }
      catch
      {
        return false;
       }
    }
      

  9.   

    public static bool IsDate(object lString)
    {
    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
      

  10.   

    iYear = Convert.ToInt32(aDate[0]);
    iMonth = Convert.ToInt32(aDate[1]);
    iDay = Convert.ToInt32(aDate[2]);并没有得到我想要的结果!!!
      

  11.   

    DataTime类能把某个日期,按某种格式显示的,这个微软的工程师早就想到了,不用自己做了
      

  12.   

    先跟踪一下aData里面的值再说。
    object lString  ?????
    iYear  = (int)aDate[0];
    iMonth = (int)aDate[1];
    iDay   = (int)aDate[2];或者是强制类形转换时地址出现混乱。晕。
      

  13.   

    比如:
    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文件中是没有什么问题的
      

  14.   

    但在IsDate Method中是错误的
      

  15.   

    这样如何,在我们的工程中这样做过(类似)
                    
    string dateString = "20061109";
    try
    {
        date= DateTime.ParseExact(dateString, "yyyyMMdd", null);
    }
    catch (FormatException ex)
    {
        return false;
    }
    return true
      

  16.   

    NodeNET(点NET)
    目的是不管什么格式日期都转换为欧洲格式(美国日期)
    ------------------------
    什么意思哦?欧洲格式和美国日期不同吧?楼主的意思是不是说,不管过来的是什么样的日期,都转为11.9.2006这种格式?  
     
      

  17.   

    这样如何,在我们的工程中这样做过(类似),格式可以订制
                    
    string dateString = "20061109";
    try
    {
        date= DateTime.ParseExact(dateString, "yyyyMMdd", null);
    }
    catch (FormatException ex)
    {
        return false;
    }
    return true
      

  18.   

    string strDT = "2006-02-15";            try
                {
                    DateTime dt = DateTime.Parse(strDT);
                    textBox1.Text = dt.ToString("MM/dd/yyyy");  -- 美国格式
                }
                catch(Exception ex)
                {
                    MessageBox.Show("Format error.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }