简体   繁体   中英

Parse datetime string with Eastern Daylight

I have a date string as "2017-08-08T17:08:29 EDT -0400". We are rewriting the Java App to C#. In Java by parsing this date to the SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss z Z"), the validation is done. But in C# when I try to parse it to DateTime I am getting an error as this is not a DateTime string. How can I validate this date using C#?

Thanks

I have few suggestions and questions:

  1. What's 2017-08-08-08?
  2. I would use either EDT or -0400, not both, eg:

     var s = "2017-08-08T17:08:29 -0400"; var format = "yyyy-MM-ddTHH:mm:ss zzz"; CultureInfo provider = CultureInfo.InvariantCulture; var d = DateTime.ParseExact(s, format, provider); Console.WriteLine(d);
  3. Better, use NodaTime and its parsers. Check out https://nodatime.org/2.4.x/userguide/offsetdatetime-patterns

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM