Get DateTime.Now for a specific TimeZone regardless of the device timezone?

33,648

Solution 1

This is a bug in MonoTouch.

The fix will be included in a future version of MonoTouch (I don't know exactly which yet though).

In any case there is already a hotfix available.

Solution 2

Use DateTime.Now. This will give you system TimeZone Date and Time. Now convert that time to desired timezone time like this

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

To get list of TimeZone 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);

Solution 3

You can do it like:

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

Just pass the given parameters.

Share:
33,648
startupsmith
Author by

startupsmith

Software developer. Currently working with C# on Mono, Silverstripe, Mono for Android and Monotouch. I have also done a lot of work deploying servers to the cloud (EC2 and Rackspace).

Updated on July 09, 2022

Comments

  • startupsmith
    startupsmith almost 2 years

    I have MonoTouch app which process data from a webservice. This data contains date information which is specific to a timezone. The timezone is UTC +12 which is for New Zealand.

    My app displays this data based on the current time. The problem with this is that when the app is used in different TimeZones the data isn't displayed properly because the current time on the device is incorrect.

    How can I get the current datetime for UTC +12 regardless of the locale/timezone setting on the device?

    Edit:

    I have 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
    
    • Nikhil Agrawal
      Nikhil Agrawal over 11 years
      @lanks: What happened? Why did you unremoved your answer. Is it not what you desired?
    • startupsmith
      startupsmith over 11 years
      @Nickhil Agrawal Sorry but after testing your answer it did not work. I have updated my question with the code and exception.
  • RJB
    RJB over 7 years
    List of MSFT time zones: msdn.microsoft.com/en-us/library/…