问题
I'm having a few strange issues with the UIDatePicker. I checked the forums and I haven't had any luck with the answers supplied.
Say I pick a date, 20/11/2010 EN_AUS locale. When I NSLog it the date property is 2010-11-19 13:00:00 +0000. But when I format it and display in a label it appears correct, i.e. 20/11/2010?!
I save this date so that the next time the user enters this view the uiDatePicker should automatically be set at the last selected date. But it just isn't happening, it still displays the current date.
I've listed the important code below, any advice is appreciated :)
// setting up the uiDatePicker
NSLocale *ozLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_AUS"];
self.datePicker.calendar = [NSCalendar autoupdatingCurrentCalendar];
[NSTimeZone setDefaultTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:39600]]; // Melbourne time zone
[self.datePicker setLocale:ozLocale];
self.datePicker.timeZone = [NSTimeZone localTimeZone];
[ozLocale release];
// displaying the date from the uidatePicker
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
self.dateLabel.text = [dateFormatter stringFromDate:self.datePicker.date];
[dateFormatter release];
// In viewWillAppear I set the uiDatePicker. lastDatePicked has the value of 2010-11-19 13:00:00 +0000 at this point
[self.datePicker setDate:self.appDelegate.lastDatePicked animated:FALSE];
回答1:
First of all, note that 2010-11-19 13:00:00 +0000
is in UTC so the date formatted for EN_AUS is going to be ahead by 11 hours which is why you see 20/11/2010
when formatting it using the locale. So that isn't a problem at all and that is the correct behaviour.
I see no problem with your setDate:animated:
line so I would make sure that self.appDelegate
and self.datePicker
are actually non-nil just above that line.
回答2:
There is a bug in Apples date time picker when the phones time zone has been set. It's been in all versions of the phone since it was released and it's been a real pain for me where I live (New Zealand with a +12/+13 GMT, which seems to magnify the problem). You can reproduce the problem anywhere a date/time is entered, for example the iphone's contact birthday field.
I've read in some places that this bit of code sometimes fixes the problem:
datePicker.timeZone = [NSTimeZone localTimeZone];
See this answer or this one for more details..
回答3:
Use this method to get correct date:
func getDateStamp(date:String)-> String{
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss +SSSS"
dateFormatter.timeZone = TimeZone(abbreviation: "UTC")
guard let date = dateFormatter.date(from: date) else {
// assert(false, "no date from string")
return ""
}
dateFormatter.dateFormat = "dd MMMM,yyyy" //"yyyy-MM-dd"
dateFormatter.timeZone = TimeZone.current
let st = dateFormatter.string(from: date)
return st
}
来源:https://stackoverflow.com/questions/4272953/uidatepicker-strange-behaviour-not-setting-date-and-wrong-date-being-selected