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

后端 未结 3 1674
温柔的废话
温柔的废话 2021-02-20 07:22

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.

相关标签:
3条回答
  • 2021-02-20 07:50

    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);
    
    0 讨论(0)
  • 2021-02-20 07:57

    You can do it like:

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

    Just pass the given parameters.

    0 讨论(0)
  • 2021-02-20 08:01

    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.

    0 讨论(0)
提交回复
热议问题