Get DateTime.Now for a specific TimeZone, regardless of the device’s time zone?

I have a MonoTouch application that processes data from a web service. This data contains date information that relates to the time zone. The time zone is UTC +12, which is designed for New Zealand.

My application displays this data based on the current time. The problem is that when the application is used in different TimeZones, the data is not displayed correctly, because the current time on the device is incorrect.

How can I get the current time-time for UTC +12 regardless of the language / time zone setting on the device?

Edit:

I tried the following code based on the answers below:

TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland")); 

This code works fine on my computer, however, when I run it in MonoTouch, I get the following exception:

 System.ArgumentException: Kind propery of dateTime is Local but the sourceTimeZone does not equal TimeZoneInfo.Local at System.TimeZoneInfo.ConvertTime (DateTime dateTime, System.TimeZoneInfo sourceTimeZone, System.TimeZoneInfo destinationTimeZone) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System/TimeZoneInfo.cs:179 at System.TimeZoneInfo.ConvertTime (DateTime dateTime, System.TimeZoneInfo destinationTimeZone) [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Core/System/TimeZoneInfo.cs:173 
+7
source share
3 answers

This is a bug in MonoTouch.

The fix will be included in a future version of MonoTouch (I don’t know for sure, though for now).

In any case, a fix already exists.

+3
source

Use DateTime.Now . This will give you the date and time of the TimeZone system. Now convert this time to the right time in the time zone, for example,

 var indianTime = TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("India Standard Time")); 

To get the TimeZone list, run this method

 ReadOnlyCollection<TimeZoneInfo> zones = TimeZoneInfo.GetSystemTimeZones(); Console.WriteLine("The local system has the following {0} time zones", zones.Count); foreach (TimeZoneInfo zone in zones) Console.WriteLine(zone.Id); 
+20
source

You can do it like:

 Datetime date = TimeZoneInfo.ConvertTime(utcDateTime, timeZone); 

Just pass these parameters.

+10
source

All Articles