Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When using DateTime.ParseExact, how do you specify what the timezone is of the given date?

Given the following:

DateTime.ParseExact(timeStamp, "yyyyMMdd-HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);

How do you specify that the given time is UTC? Right now the result is giving it my current timezone.

like image 421
CoolUserName Avatar asked Dec 28 '22 18:12

CoolUserName


2 Answers

Add DateTimeStyles.AssumeUniversal, since it's not specified in the format.

DateTime.ParseExact(timeStamp, "yyyyMMdd-HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal);
like image 154
agent-j Avatar answered Dec 30 '22 06:12

agent-j


You can include the timezone offset at the end of the parse string like so

DateTime.Parse("2011-01-01 12:00:00-5:00")

http://msdn.microsoft.com/en-us/library/1k1skd40.aspx

like image 37
CraigW Avatar answered Dec 30 '22 06:12

CraigW