Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET converting datetime to UTC given the timezone

Using C#, I need to convert incoming datetime values into UTC. I know there is functionality in .NET for these conversions but all I have to identify the timezone is the standard timezone list

http://www.timegenie.com/timezones

What's the best way to do this in .NET? Will I need to create a mapping table to convert the timezones to the IDs in TimeZoneInfo.GetSystemTimeZones() (e.g. "Pacific Standard Time (Mexico)") so that I can then use TimeZoneInfo.FindSystemTimeZoneById()?

Thanks for any help

like image 405
Bobbler Avatar asked Aug 04 '11 23:08

Bobbler


People also ask

How do you convert DateTime to timezone in UTC?

The ToUniversalTime method converts a DateTime value from local time 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.

How do I get UTC time in C#?

ToUniversalTime() Method in C# This method is used to convert the value of the current DateTime object to Coordinated Universal Time (UTC). Syntax: public DateTime ToUniversalTime ();

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

ConvertTime(DateTimeOffset, TimeZoneInfo) method that performs time zone conversions with ToOffset(TimeSpan) values. The method's parameters are a DateTimeOffset value and a reference to the time zone to which the time is to be converted. The method call returns a DateTimeOffset value.

How do I convert DateTime to timezone?

To do this, we will use the FindSystemTimeZoneById() of TimeZoneInfo class. This function takes Id as a parameter to return the equivalent time in timezone passed. In the example below, we are taking the current system time and converting it into “Central Standard Time”. DateTime currentTime = TimeZoneInfo.


3 Answers

DateTime.ToUniversalTime Method

Or

DateTime.UtcNow Property

like image 185
Peyman Avatar answered Oct 08 '22 09:10

Peyman


I've done it before by storing the timezone id in the database using a mapping table. i.e. A table containing the results of TimeZone.GetSystemTimeZones()

You don't actually need to use TimeZoneInfo.FindSystemTimeZoneById() though: you can do the conversion using one of the overloads of TimeZoneInfo.ConvertTimeBySystemTimeZoneId(). This method has some overloads which take DateTime values, and some that take DateTimeOffset values (which are preferable as they specify an unambiguous point in time).

e.g.

TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.Now, "New Zealand Standard Time", "UTC")

The real benefit of using the system time zone id rather than an offset stored in the database is that daylight savings time is automatically handled for you by TimeZoneInfo.ConvertTimeBySystemTimeZoneId.

like image 30
Bennor McCarthy Avatar answered Oct 08 '22 09:10

Bennor McCarthy


There is a lot of information on MSDN about converting to different time zones.

  • DateTime (MSDN), specifically look at 'DateTime Operations'.
  • TimeZoneInfo

You probably want to use ConvertTimeBySystemTimeZoneId method (follow link for examples).

like image 28
Dennis Avatar answered Oct 08 '22 11:10

Dennis