1

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];
bennythemink
  • 5,096
  • 4
  • 36
  • 54
  • This is not sufficient code.You must show that code where you save this time and format of date during save. declration of lastDatePicked Property. – Ishu Nov 25 '10 at 05:40

3 Answers3

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..

Community
  • 1
  • 1
Shane Powell
  • 13,698
  • 2
  • 49
  • 61
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.

Mike Weller
  • 45,401
  • 15
  • 131
  • 151
  • Hey Mike, thanks for explaining the formatting issue. I thought it was a bug but now it makes perfect sense! the appDelegate and datePicker are not nil as you suggested. stumped on this one :( thanks for your advice though. – bennythemink Nov 28 '10 at 02:28
0

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

}

Sandeep Yadav
  • 114
  • 2
  • 6