2012-06-22 53 views
0

我正在使用UIDatePicker,當它選擇上午7:00時,它顯示在UILableView上午6:00。我如何設置UIDatePicker以顯示正確的時間?NSDatePicker顯示錯誤的時間

+0

可能重複(http://stackoverflow.com/questions/8466744/getting-date-from-nsdate-date-off-通過-A-幾個小時) –

回答

5
NSLocale *locale = [NSLocale currentLocale]; 
[dateFormatter setLocale:locale]; 

self.datePicker.locale = locale; 

集裏面這在程序

0

設置標籤的文本價值

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { 
    // You have the selected row, update the text field. 
} 
2

我覺得你的日期選擇器可能會選擇合適的日期,但你標籤不顯示它正確。使用NSDateFormatter來設置正確的語言環境並根據需要設置日期的格式。 [由幾個小時來往從\ [NSDate的日期\]斷日期]的

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
formatter.locale = [NSLocale currentLocale]; 
formatter.dateStyle = NSDateFormatterMediumStyle; // e.g. 
formatter.timeStyle = NSDateFormatterMediumStyle; 

myLabel.text = [formatter stringFromDate:dateFromPicker];