4

I have date that I get from incoming API call: Wed, 6 Mar 2019 14:39:49 +0300

I need to parse this string to DateTime. For this I'm using the following code:

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                     new string[] { "ddd, dd MMM yyyy HH:mm:ss zzzz" },
                     CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

But as a result I have error:

String 'Wed, 6 Mar 2019 14:39:49 +0300' was not recognized as a valid DateTime.

What am I doing wrong? How can I resolve this?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
A. Gladkiy
  • 3,134
  • 5
  • 38
  • 82
  • 1
    It seems like the problem is with the single digit (6) doesn't match the `dd` pattern. "06" should probably work – haim770 Mar 06 '19 at 14:16
  • 2
    'Long date format' is not a good fit for APIs, it would be better to use ISO8601 formats is at all possible, which is language independent. If your code is running in a non-English locale, then it will not parse correctly. If you have any control over the API, I would recommend you fix it. – Neil Mar 06 '19 at 14:19
  • 1
    FYI: Not directly related to your problem, but you don't need to use a string array if you want to use/provide only a single date format string... –  Mar 06 '19 at 14:25

7 Answers7

13

I see 2 things;

  1. You should use d specifier instead of dd specifier since your single digit day number does not have a leading zero.
  2. There is no zzzz as a custom format specifier. You should use zzz specifier instead.

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                     new string[] { "ddd, d MMM yyyy HH:mm:ss zzz" },
                     CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

But honestly, if your strings have a UTC Offset value, I would suggest parse it to DateTimeOffset instead since a DateTime instance does not have offset part and using zzz specifiers is not recomended as stated on MSDN.

With DateTime values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. It does not reflect the value of an instance's DateTime.Kind property. For this reason, the "zzz" format specifier is not recommended for use with DateTime values.

To parse DateTimeOffset,

DateTimeOffset.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                           new string[] { "ddd, d MMM yyyy HH:mm:ss zzz" },
                           CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);

Now you can use it's .DateTime and/or .Offset properties separately if you want.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

Change "ddd, dd MMM yyyy HH:mm:ss zzzz" to "ddd, d MMM yyyy HH:mm:ss zzzz"

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", 
                     new string[] { "ddd, d MMM yyyy HH:mm:ss zzzz" },
                     CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
gari
  • 95
  • 7
1

You might be looking for

DateTime myDate = DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300",
                                      "ddd, d MMM yyyy HH:mm:ss zzz",
                                      CultureInfo.InvariantCulture, 
                                      DateTimeStyles.AdjustToUniversal);

Refer: https://stackoverflow.com/a/10426999/4373895 This would also help you.Thanks.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
VikrantMore
  • 903
  • 7
  • 25
1

Datetime.ParseExact function just like below code withh help you. One thing needs to be changed is instead of dd MMM yyyy use d MMM yyyy.

DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", "ddd, d MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
1

d MMM yyyy instead of ddd MMM yyyy

date = DateTime.ParseExact("Wed, 6 Mar 2019 14:39:49 +0300", "ddd, d MMM yyyy HH:mm:ss zzz", CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal);
0

The format for the UTC offset is zzz not zzzz and expects it to have a colon (:) to separate the hours from the minutes (such as +03:00). As well as the dd is for leading zero day of month but you have a single digit for the day of month. In that case use d.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • unfortunately it didn't help and I can't add colon (`:`) as it's 3-rd party service. – A. Gladkiy Mar 06 '19 at 14:16
  • @A.Gladkiy you can do some preprocessing on the string. – Daniel A. White Mar 06 '19 at 14:16
  • @DanielA.White Actually `zzz` specifier _can_ parse without `:` as well. From wikipedia: "It is generally shown in the format `±[hh]:[mm]`, `±[hh][mm]`, or `±[hh]`". That colon is not mandatory as far as I can see. – Soner Gönül Mar 06 '19 at 14:26
0
DateTime time = Convert.ToDateTime("Wed, 6 Mar 2019 14:39:49 +0300");
string ti = time.ToString(@"ddd, dd MMM yyyy HH:mm:ss zzzz");
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364