16

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Warz
  • 7,386
  • 14
  • 68
  • 120
  • How about `var dt = DateTime.ParseExact(s, "MMM dd, yyyy H:mm:ss tt 'CDT'", null);` ? – Vlad Aug 08 '12 at 21:54

3 Answers3

10

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

rumburak
  • 1,097
  • 11
  • 19
  • 4
    what happens if CDT has daylight savings time and becomes -4? – Greg Aug 09 '12 at 01:55
  • where do i find the information about off sets and Timezone, i may also get CST as a timezone and need to replace and handle daylight savings time. – Warz Aug 09 '12 at 15:25
  • [Central Time Zone](http://en.wikipedia.org/wiki/Central_Time_Zone_(North_America)#Central_Daylight_Time), [UTC time offsets](http://en.wikipedia.org/wiki/List_of_UTC_time_offsets), [time zone offset](http://www.physics.sfasu.edu/astro/jupiter/offset.html) – rumburak Aug 09 '12 at 17:14
  • to convert this date to UTC, cant i just dt.ToUniversalTime() ? – Warz Aug 09 '12 at 21:18
  • I think you can. Once you convert the string into a DateTime then you should be able to do .ToUniversalTime(). You need to have it in the right format first though (including daylight savings) – Greg Aug 10 '12 at 03:58
  • @Greg could you explain your first comment, "daylight savings time becoming -4", i thought CDT was UTC-5.00 and CST was UTC-6.00 ? – Warz Aug 13 '12 at 15:11
  • I quite possibly went the wrong way. I live in Sydney and we're UTC +10 normally but in summer UTC+11. I'm not claiming to know the specific timezones, just raising the fact that you can't just use UTC-5, you need to change for daylight savings. – Greg Aug 13 '12 at 20:05
  • @Warz: CDT time is -5, please consider reading [CDT wiki document](http://en.wikipedia.org/wiki/Central_Daylight_Time) – rumburak Aug 13 '12 at 20:08
  • 2
    Please note that `IsDaylightSavingTime` returns true if the time is daylight time in the `TimeZoneInfo.Local` timezone, **not** the one that the `DateTime` instance you call the method on is set to. How awful is that? If the machine running the app is in UTC, the method always returns `false` – Josh Jun 09 '20 at 18:23
8

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

Community
  • 1
  • 1
  • 4
    It's correct to use UTC because it is the international standard, but it GMT is not ambiguous for daylight saving. GMT is always UTC+0. The UK switches to BST during the summer. More info at http://www.differencebetween.com/difference-between-gmt-and-utc/ – thelem Nov 22 '17 at 12:57
0

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);
mortenma71
  • 1,078
  • 2
  • 9
  • 27