我有从Web服务处理数据的MonoTouch应用程序。此数据包含特定于时区的日期信息。时区是UTC +12,用于新西兰。获取DateTime.Now为特定的时区而不管设备的时区?
我的应用程序根据当前时间显示此数据。问题在于,当应用程序在不同时区使用时,数据显示不正确,因为设备上的当前时间不正确。
无论设备上的语言环境/时区设置如何获取UTC +12的当前日期时间?
编辑:
我曾尝试基于下面的答案下面的代码:
TimeZoneInfo.ConvertTime (DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Pacific/Auckland"));
此代码的工作在我的电脑上罚款但是当我运行它在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
以DateTime.UtcNow开头? –
@lanks:发生了什么?你为什么不去除你的答案。这不是你想要的吗? –
@Nickhil Agrawal对不起,但经过测试你的答案它没有奏效。我用代码和例外更新了我的问题。 – startupsmith