时区问题与UIDatePicker日期:1小时错误(Timezone issue with UIDatePicker Date: 1 Hour Wrong)

编程入门 行业动态 更新时间:2024-10-25 02:19:51
时区问题与UIDatePicker日期:1小时错误(Timezone issue with UIDatePicker Date: 1 Hour Wrong)

Monotouch UIDatePicker在1小时后出现问题。 我认为这与时区或类似的事情有关。 我试着明确地设置我的UIDatePicker的TimeZone和Locale,但这似乎没有帮助。

datePicker.Locale = NSLocale.CurrentLocale; datePicker.TimeZone = NSTimeZone.LocalTimeZone;

在ValueChanged处理程序中,以下行返回比用户界面中选择的时间提前1小时的值:

var date = DateTime.SpecifyKind((s as UIDatePicker).Date, DateTimeKind.Local).ToLocalTime();

在ValueChanged处理程序中,我仔细检查了Locale和TimeZone与设置的相同。 时区是欧洲/都柏林和地区en_US。 这些信息被检索到:

datePicker.Locale.LocaleIdentifier; datePicker.DatePicker.TimeZone;

我错过了另一个步骤吗?

谢谢!

I'm having an issue with the Monotouch UIDatePicker being 1 hour behind. I think this is to do with Time Zones or something similar. I've tried explicitly setting the TimeZone and Locale of my UIDatePicker but this doesn't seem to be helping.

datePicker.Locale = NSLocale.CurrentLocale; datePicker.TimeZone = NSTimeZone.LocalTimeZone;

In the ValueChanged handler the following line returns a value 1 hour earlier than the time selected in the user interface:

var date = DateTime.SpecifyKind((s as UIDatePicker).Date, DateTimeKind.Local).ToLocalTime();

In the ValueChanged Handler I've double checked that the Locale and TimeZone is the same as what was set. The TimeZone is Europe/Dublin and Locale en_US. This information was retrieved by:

datePicker.Locale.LocaleIdentifier; datePicker.DatePicker.TimeZone;

Is there another step I'm missing?

Thanks!

最满意答案

从DatePicker返回的日期是UTC格式。 有几种将UTC转换为本地时间的方法。 由于这个答案表明ToLocalTime是最好的。

DateTime.SpecifyKind(datePicker.Date, DateTimeKind.Utc).ToLocalTime();

The date returned from DatePicker is in UTC format. There are several methods of converting UTC to local time. As this answer states ToLocalTime is the best one.

DateTime.SpecifyKind(datePicker.Date, DateTimeKind.Utc).ToLocalTime();

更多推荐

本文发布于:2023-08-03 18:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1396280.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时区   错误   日期   小时   UIDatePicker

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!