C# 无论设备时区如何,获取特定时区的 DateTime.Now?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12434766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:16:25  来源:igfitidea点击:

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

c#.netmonoxamarin.ios

提问by startupsmith

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.

我有 MonoTouch 应用程序,它处理来自网络服务的数据。此数据包含特定于时区的日期信息。时区为 UTC +12,即新西兰。

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?

无论设备上的语言环境/时区设置如何,如何获取 UTC +12 的当前日期时间?

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:

此代码在我的计算机上运行良好,但是当我在 MonoTouch 中运行它时,出现以下异常:

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

采纳答案by Rolf Bjarne Kvinge

This is a bugin MonoTouch.

这是MonoTouch 中的一个错误

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

该修复程序将包含在 MonoTouch 的未来版本中(不过我还不知道具体是哪个版本)。

In any case there is already a hotfixavailable.

无论如何,已经可用的修补程序

回答by Nikhil Agrawal

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

使用DateTime.Now. 这将为您提供系统时区日期和时间。现在将该时间转换为所需的时区时间,如下所示

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);

回答by Erre Efe

You can do it like:

你可以这样做:

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

Just pass the given parameters.

只需传递给定的参数。