i have a string, as an example:
"2022-04-17 14:46:31 UTC"
And i want to convert it to date time but i have got the following error:
{"The string ‘2022-04-17 14:46:31 UTC’ was not recognized as a valid DateTime. There is an unknown word starting at index ’20’."}
Notice that i have tried:
var date = DateTime.Parse("2022-04-17 14:46:31 UTC")
2.DateTime dt; DateTime.TryParseExact("2022-04-17 14:46:31 UTC", "yyyy-MM-dd hh:mm:ss 'UTC'", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt)
>Solution :
There are two things to be done:
- as mentioned in the comments: use
HHfor the 24 hour format - adjust the result to an actual UTC time
DateTime.TryParseExact("2022-04-17 14:46:31 UTC", "yyyy-MM-dd HH:mm:ss 'UTC'", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt);
DateTime utc = new DateTime(dt.Ticks, DateTimeKind.Utc);