Get DateTime.Now for a specific TimeZone regardless of the device timezone?(无论设备时区如何,获取特定时区的 DateTime.Now?)
问题描述
我有处理来自网络服务的数据的 MonoTouch 应用程序.此数据包含特定于时区的日期信息.时区是新西兰的 UTC +12.
我的应用根据当前时间显示此数据.这样做的问题是,当应用程序在不同的时区使用时,数据无法正确显示,因为设备上的当前时间不正确.
无论设备上的区域设置/时区设置如何,如何获取 UTC +12 的当前日期时间?
我根据以下答案尝试了以下代码:
TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
此代码在我的计算机上运行良好,但是当我在 MonoTouch 中运行它时出现以下异常:
System.ArgumentException: dateTime 的种类属性是 Local 但 sourceTimeZone 不等于 TimeZoneInfo.Local在 System.TimeZoneInfo.ConvertTime (DateTime dateTime, System.TimeZoneInfo sourceTimeZone, System.TimeZoneInfo destinationTimeZone) [0x00018] 在/Developer/MonoTouch/Source/mono/mcs/class/System.Core/System/TimeZoneInfo.cs:179在 System.TimeZoneInfo.ConvertTime (DateTime dateTime, System.TimeZoneInfo destinationTimeZone) [0x00000] 在/Developer/MonoTouch/Source/mono/mcs/class/System.Core/System/TimeZoneInfo.cs:173
这是一个 错误 在 MonoTouch 中.
此修复程序将包含在 MonoTouch 的未来版本中(虽然我还不知道具体是哪个).
无论如何,已经有修补程序可用.p>
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
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.
这篇关于无论设备时区如何,获取特定时区的 DateTime.Now?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无论设备时区如何,获取特定时区的 DateTime.Now?
基础教程推荐
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- MS Visual Studio .NET 的替代品 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01