Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a datetime to specific timezone in c#?

Tags:

c#

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.

like image 422
Chace Fields Avatar asked Mar 02 '13 01:03

Chace Fields


People also ask

Does DateTime have timezone?

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.

How do you change from one time zone to another?

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.

How do you convert DateTime to UTC?

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.

How do I convert one time zone to another in C #?

TimeZoneInfo timeZone = TimeZoneInfo. FindSystemTimeZoneById("Eastern Standard Time"); var time = timeZoneInfo. ConvertTime(gmTime, timeZone);


2 Answers

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).

like image 195
MerickOWA Avatar answered Sep 21 '22 17:09

MerickOWA


Try something like the following Chace

TimeZoneInfo estTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
DateTime estDateTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, estTimeZone);
like image 21
MethodMan Avatar answered Sep 21 '22 17:09

MethodMan