如题.直接用DateTime.Parse会报错.有没有什么好的方法可以解析所有的DateTime的字符串?

解决方案 »

  1.   

    System.Globalization.DateTimeFormatInfo myDTFI = new System.Globalization.CultureInfo( "en-US", false ).DateTimeFormat;Response.Write(DateTime.Now.ToString("F", myDTFI)+"<br>");string str="Mon, 28 Nov 2005 17:55:58 GMT";
    Response.Write(Convert.ToDateTime(str).ToString("d",myDTFI));
    /*
    This code produces the following output.FORMAT  en-US EXAMPLE
    CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANYd     1/3/2002
    M/d/yyyy (ShortDatePattern)D     Thursday, January 03, 2002
    dddd, MMMM dd, yyyy (LongDatePattern)f     Thursday, January 03, 2002 12:00 AMF     Thursday, January 03, 2002 12:00:00 AM
    dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)g     1/3/2002 12:00 AMG     1/3/2002 12:00:00 AMm     January 03
    MMMM dd (MonthDayPattern)M     January 03
    MMMM dd (MonthDayPattern)r     Thu, 03 Jan 2002 00:00:00 GMT
    ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)R     Thu, 03 Jan 2002 00:00:00 GMT
    ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)s     2002-01-03T00:00:00
    yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)t     12:00 AM
    h:mm tt (ShortTimePattern)T     12:00:00 AM
    h:mm:ss tt (LongTimePattern)u     2002-01-03 00:00:00Z
    yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)U     Thursday, January 03, 2002 8:00:00 AMy     January, 2002
    MMMM, yyyy (YearMonthPattern)Y     January, 2002
    MMMM, yyyy (YearMonthPattern)*/
      

  2.   

    GMT的时间直接parse是没有问题,但是如果是CST的时间,parse是会出异常的,你的方法也是不行的.
      

  3.   

    System.Globalization.DateTimeFormatInfo myDTFI = new System.Globalization.CultureInfo( "en-us", false ).DateTimeFormat;Response.Write(DateTime.Now.ToString("F", myDTFI)+"<br>");string str="Mon, 28 Nov 2005 17:55:58 CST";
    str=str.Replace("CST","GMT");//因为CST=GMT+8.所以可以先把它转换成gmt的,然后加8小时就可以了Response.Write(Convert.ToDateTime(str).AddHours(8).ToString("f",myDTFI));