I need help converting a DateTime
to a specific time zone. What I have below is not working correctly.
gmTime
= 03/02/2013 1:00:00 AM
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var time = timeZoneInfo.ConvertTime(gmTime, timeZone);
When I debug the value of time
, which should be 03/01/2013 8:00:00 PM
when the zone is applied, it comes back as 03/02/2013 1:00:00 AM
.
If I do time.ToLocalTime()
then I get the correct value. However, I need to convert time
to different time zones.
DateTime itself contains no real timezone information. It may know if it's UTC or local, but not what local really means. DateTimeOffset is somewhat better - that's basically a UTC time and an offset.
Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.
To convert the time in a non-local time zone to UTC, use the TimeZoneInfo. ConvertTimeToUtc(DateTime, TimeZoneInfo) method. To convert a time whose offset from UTC is known, use the ToUniversalTime method. If the date and time instance value is an ambiguous time, this method assumes that it is a standard time.
TimeZoneInfo timeZone = TimeZoneInfo. FindSystemTimeZoneById("Eastern Standard Time"); var time = timeZoneInfo. ConvertTime(gmTime, timeZone);
DateTime objects have a "Kind" variable which helps TimeZoneInfo know how to treat it. In the MSDN documentation for TimeZone.ConvertTime it has the following:
DateTimeKind.Local, Converts the local time to the time in destinationTimeZone.
DateTimeKind.Utc, Converts Coordinated Universal Time (UTC) to the time in destinationTimeZone.
DateTimeKind.Unspecified, Assumed to be Local.
For example:
Console.WriteLine("Local time zone is '{0}'.", TimeZoneInfo.Local.Id);
var gmTime = new DateTime(2013, 03, 02, 01, 00, 00, DateTimeKind.Utc);
var localTime = new DateTime(2013, 03, 02, 01, 00, 00, DateTimeKind.Local);
var unspecifiedTime = new DateTime(2013, 03, 02, 01, 00, 00);
var timeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var gmTimeConverted = TimeZoneInfo.ConvertTime(gmTime, timeZone); // 03/02/2013 8:00:00AM
var localTimeConverted = TimeZoneInfo.ConvertTime(localTime, timeZone); // 03/02/2013
var unspecifiedTimeConverted = TimeZoneInfo.ConvertTime(unspecifiedTime, timeZone);
Console.WriteLine("Converting GMT to EST: {0}", gmTimeConverted);
Console.WriteLine("Converting Local to EST: {0}", localTimeConverted);
Console.WriteLine("Converting Unspecified to EST: {0}", unspecifiedTimeConverted);
Results in:
Local time zone is 'Pacific Standard Time'. Converting GMT to EST: 3/1/2013 8:00:00 PM Converting Local to EST: 3/2/2013 4:00:00 AM Converting Unspecified to EST: 3/2/2013 4:00:00 AM
Or if your local timezone is 'Eastern Standard Time' you get these results
Local time zone is 'Eastern Standard Time'. Converting GMT to EST: 3/1/2013 8:00:00 PM Converting Local to EST: 3/2/2013 1:00:00 AM Converting Unspecified to EST: 3/2/2013 1:00:00 AM
If you'd like TimeZoneInfo to treat 'Unspecified' like Utc, you should function like TimeZoneInfo.ConvertTimeFromUtc. Again from MSDN documentation
DateTimeKind.Local, Throws an ArgumentException.
DateTimeKind.Unspecified or DateTimeKind.Utc, Converts from Coordinated Universal Time (UTC).
Try something like the following Chace
TimeZoneInfo estTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime estDateTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, estTimeZone);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With