Jesse
Jesse

Reputation: 1940

Parse GMT DateTIme

I have 03-10-14 18:44:58. The time portion is GMT where 03 = day 10 = month = 14 = year and 18 = hour 44 = minute 58 = seconds

How can I parse this out? This is what I'm using but its not working:

var date = "03-10-14 18:44:58";
_Packet.Time = DateTime.ParseExact(datetime, "dd-MM-yy HH:mm:ss 'GMT'", CultureInfo.CurrentCulture, DateTimeStyles.AllowWhiteSpaces);

Upvotes: 1

Views: 837

Answers (1)

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51330

Of course it doesn't work, since the parsing pattern does not match the date string... Your input string doesn't contain the string GMT.

Try this instead:

var date = DateTime.ParseExact(datetime, "dd-MM-yy HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal);

Here, I used the AssumeUniversal and AdjustToUniversal flags. Used together, thay'll produce a UTC date.

If you want to convert that to local time, well:

date = date.ToLocalTime();

Upvotes: 5

Related Questions