我遇到了一个关于
但是使用
以下是我所有的代码!
UIDatePicker
的奇怪问题。我正在尝试制作一个应用程序,使我可以根据其他时区设置提醒。例如,在纽约时间下午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。