DateTime.ParseExact format string

后端 未结 3 1759
清酒与你
清酒与你 2020-12-21 13:32

I have a web application that passes a DateTime from one page to another through the query string. It was working just fine in both IE and FireFox, but was throwing excepti

3条回答
  •  清酒与你
    2020-12-21 13:45

    It may just be that your format doesn't cover the (Eastern Daylight Time) section. Try parsing that out of your string using regular string handling methods, then calling ParseExact on the remainder.

    Edit: As Oded points out, you'll also have to put the GMT into your format string as a literal:

    "ffffd MMM dd yyyy HH:mm:ss 'GMT'zzz"
    

    The following works:

    var input = "Wed Oct 03 2012 08:00:00 GMT-0400 (Eastern Daylight Time)";
    var trim = input.Substring(0, input.IndexOf(" ("));
    var dt = DateTime.ParseExact(
        trim,
        "ffffd MMM dd yyyy HH:mm:ss 'GMT'zzz",
        CultureInfo.InvariantCulture);
    

提交回复
热议问题