是否获取DateTime.Now以获得特定的时区,而与设备时区无关? [英] Get DateTime.Now for a specific TimeZone regardless of the device timezone?

查看:402
本文介绍了是否获取DateTime.Now以获得特定的时区,而与设备时区无关?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有MonoTouch应用程序,该应用程序处理来自Web服务的数据.该数据包含特定于时区的日期信息.时区为UTC +12,适用于新西兰.

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.

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

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

我已经根据以下答案尝试了以下代码:

I have tried the following code based on the answers below:

TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));

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

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

推荐答案

这是一个错误在MonoTouch中.

This is a bug in MonoTouch.

此修复程序将包含在将来的MonoTouch版本中(虽然我尚不完全清楚).

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

无论如何,已经有修复程序.

这篇关于是否获取DateTime.Now以获得特定的时区,而与设备时区无关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆