Home > Mobile >  C# DateTime.ParseExact 000 0000
C# DateTime.ParseExact 000 0000

Time:09-23

I tried to find an answer in other forums or questions but I cannot find an answer to my problem

I trying to convert a string to DateTime

This is my string: 1999-12-31T23:00:00.000 0000

I'm using this line (after trying a lot of options)

DateTime.ParseExact("1999-12-31T23:00:00.000 0000", "yyyy-MM-ddThh:mm:ss:000 ZZZZZ", CultureInfo.InvariantCulture)

Thanks for helping me

CodePudding user response:

Not 100% sure, try this.

var str = "1999-12-31T23:00:00.000 0000";
var parts = str.Split(' ');
var date = DateTime.Parse(parts[0]);
var offset = TimeSpan.Parse(parts[1]);
var dto = new DateTimeOffset(date, offset);

CodePudding user response:

Try "yyyy'-'MM'-'dd'T'HH:mm:ss.fffzzzz" format string:

DateTime result = DateTime.ParseExact("1999-12-31T23:00:00.000 0000", 
                                      "yyyy'-'MM'-'dd'T'HH:mm:ss.fffzzzz", 
                                       CultureInfo.InvariantCulture);

please note:

  • 'T' - escapement with '...'
  • . - decimal separator for fraction of second
  • HH - hour in 24 format (00..23 range)
  • fff - for second fraction
  • Related