0

i am trying to parse this date 14.03.2014 22:16:23 using DateTimeOffset.ParseExact but i get String was not recognized as a valid DateTime

What i already tried:

DateTimeOffset.ParseExact("14.03.2014 22:16:23", "G", new CultureInfo("en-US"))
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Roman Ratskey
  • 5,101
  • 8
  • 44
  • 67

1 Answers1

1

You need to define your custom format pattern like;

var date = DateTimeOffset.ParseExact("14.03.2014 22:16:23",
                                     "dd.MM.yyyy HH:mm:ss",
                                      new CultureInfo("en-US"));

Output will be;

3/14/2014 10:16:23 PM +00:00

Here a demonstration.

From it's documentation;

A format specifier that defines the expected format of input.

Your G format specifier does not work on this case because it is a standar date and time format. If you really want to use it, you can do;

For example;

var off  = DateTimeOffset.Parse("14.03.2014 22:16:23");
Console.WriteLine(off.ToString("G"));
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364