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

后端 未结 3 1615
温柔的废话
温柔的废话 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条回答
  •  梦毁少年i
    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 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);
    

提交回复
热议问题