2011-08-27 33 views
5

我在我的nib文件中有一個NSDatePicker。在代碼中,我設置它的時區是GMT日曆的時區:關於這一點我填充程序的日期選擇器NSDatePicker時區怪異

[datePicker setTimeZone:[calendar timeZone]]; 

我只真正關心的時間(小時,分):

NSDate *anyDate = [NSDate date]; 
     NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anyDate]; 
     [components setHour:hours]; 
     [components setMinute:minutes]; 
     NSDate *newDate = [calendar dateFromComponents:components]; 
     [datePicker setDateValue:newDate]; 

這正確具有將日期選擇器時間設置爲我想要的時間的預期效果。因此,如果小時數是8,分鐘數是30,則日期選擇器顯示8:30。然而,如果我在日期選擇器的小時字段中輸入8,它將顯示爲3.某些奇怪的事情正在與時區相關,但我不知道在哪裏...

+0

你有日期格式化程序連接到日期選擇器嗎? – jtbandes

回答

8

看起來像我不是第一個人偶然發現這個問題。對於那些有興趣的解決方案,我在這裏找到了http://www.cocoabuilder.com/archive/cocoa/305254-nsdatepicker-weirdness-with-time.html。顯然,如果您設置日期選擇器的時區而不是日曆,則會出現此問題。分辨率是這樣的:

[datePicker setTimeZone:[calendar timeZone]]; 
[datePicker setCalendar:calendar]; 
+1

剛剛幫我解決了同樣的問題 - 感謝您發佈您的發現! –

+0

將日期選擇器的「語言環境」設置爲當前語言環境確實更簡單,更正確。 – matt