I need to convert date from headers to C# object. I obtain that header as a string. How could I convert string (Fri, 16 May 2015 05:50:06 GMT) to DateTime ?
I tried the following (or dozens others), but that didn't work
DateTime.TryParseExact(date, "ddd, dd MMM yyyy hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)
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?
I see 2 things;
You should use d specifier instead of dd specifier since your single digit day number does not have a leading zero.
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.
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);
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.
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);
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);
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.
DateTime time = Convert.ToDateTime("Wed, 6 Mar 2019 14:39:49 +0300");
string ti = time.ToString(#"ddd, dd MMM yyyy HH:mm:ss zzzz");
I have a string s = "May 16, 2010 7:20:12 AM CDT that i want to convert into a DateTime object. In the code below i get a Date format cannot be converted error when attempting to parse the text with a known format.
timeStamp = matches[0].Groups[1].Value;
dt = DateTime.ParseExact(timeStamp, "MMM dd, yyyy H:mm:ss tt", null);
The timezone comes in as CDT UTC... and i think is whats causing the problem or my format?
Central Daylight Time
Try this:
string dts = "May 16, 2010 7:20:12 AM CDT";
DateTime dt =
DateTime.ParseExact(dts.Replace("CDT", "-05:00"), "MMM dd, yyyy H:mm:ss tt zzz", null);
EDIT:
For daylight savings time please consider DateTime.IsDaylightSavingTime and TimeZone.CurrentTimeZone
Custom Date and Time Format Strings
Make sure the DateTime is unambiguously DateTimeKind.Utc. Avoid "GMT", it is ambiguous for daylight saving.
var dt = new DateTime(2010, 1, 1, 1, 1, 1, DateTimeKind.Utc);
string s = dt.ToLocalTime().ToString("MMM dd, yyyy HH:mm:ss tt \"GMT\"zzz");
it's gives output : Dec 31, 2010 19:01:01 pm GMT-06:00
For more detail refer this Link
I convert my date string with timezone "Thu, 22 Sep 2022 06:38:58 +0200" using "ddd, dd MMM yyyy HH:mm:ss zzz":
var dateString = "Thu, 22 Sep 2022 06:38:58 +0200";
string[] acceptedDateFormats = { "ddd, dd MMM yyyy HH:mm:ss zzz" };
var dateParsed = DateTime.TryParseExact(dateString, acceptedDateFormats,
CultureInfo.InvariantCulture, DateTimeStyles.None, out var date);
Can someone tell me how should I approach converting the following format to a proper DateTime object?
11:50:46 AM on Wednesday, October 19, 2011
string s = "11:50:46 AM on Wednesday, October 19, 2011";
DateTime dateTime = DateTime.ParseExact(s,
"hh:mm:ss tt on dddd, MMMM dd, yyyy", CultureInfo.InvariantCulture);
how would you convert this date into c# Datetime object?
Mon Mar 16 14:21:27 +0000 2009
i tried parseexact with format = "ddd MMM dd hh:mm:ss zzzz yyyy" but it didn't work.
what did i do wrong?
For 24-hour hours, you want HH, not hh:
DateTime when = DateTime.ParseExact("Mon Mar 16 14:21:27 +0000 2009",
"ddd MMM dd HH:mm:ss zzzz yyyy",
CultureInfo.InvariantCulture);