我遇到了一个奇怪的问题UIDatePicker
。我正在尝试制作一个允许我根据其他时区设置提醒的应用程序。IE。提醒我纽约下午 4 点。
使用 时UIDatePicker
,返回的NSDate
对象始终是手机时区中的选定时间,转换为 UTC。它不会尊重设置为另一个时区的时区或日历属性。
这是我所有的荣耀代码!
NSLog(@"Date: %@", [datePicker date]);
NSLog(@"DatePicker TimeZone: %@", [datePicker timeZone]);
NSLog(@"Calendar TimeZone: %@", [datePicker.calendar timeZone]);
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"America/New_York"];
NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[cal setTimeZone:timeZone];
NSLog(@"New Calendar TimeZone: %@", [cal timeZone]);
NSLog(@"Updating timezones");
[datePicker setTimeZone:timeZone];
[datePicker setCalendar:cal];
NSLog(@"Date: %@", [datePicker date]);
NSLog(@"DatePicker TimeZone: %@", [datePicker timeZone]);
NSLog(@"Calendar TimeZone: %@", [datePicker.calendar timeZone]);
并且输出...
2012-11-09 14:13:04.834 DatePickerTest[4524:c07] Date: 2012-11-09 03:12:59 +0000
2012-11-09 14:13:04.835 DatePickerTest[4524:c07] DatePicker TimeZone: (null)
2012-11-09 14:13:04.837 DatePickerTest[4524:c07] Calendar TimeZone: Australia/Sydney (GMT+11:00) offset 39600 (Daylight)
2012-11-09 14:13:04.837 DatePickerTest[4524:c07] New Calendar TimeZone: America/New_York (EST) offset -18000
2012-11-09 14:13:04.837 DatePickerTest[4524:c07] Updating timezones
2012-11-09 14:13:04.882 DatePickerTest[4524:c07] Date: 2012-11-09 03:12:59 +0000
2012-11-09 14:13:04.882 DatePickerTest[4524:c07] DatePicker TimeZone: America/New_York (EST) offset -18000
2012-11-09 14:13:04.883 DatePickerTest[4524:c07] Calendar TimeZone: America/New_York (EST) offset -18000
返回的NSDate
第一行是正确的,它是悉尼 14:12 的 UTC 时间,但是更改时区和日历后,返回的日期仍然是基于悉尼 14:12,而不是 14:13纽约。