MTA MTA - 26 days ago 21
Objective-C Question

UIDatePicker return wrong NSDate

I am using UIDatePicker in my app and when i take the date that was chosen with:

NSDate *date = picker.date;


picker.date
returned the day before the date that I chose.

any idea why it happens?

Answer

UIDatePicker will be displaying dates and times in your local timezone. However, NSDate does not have any concept of a timezone as it stores an absolute number of seconds since a reference date. When NSLogging a date, it shows the date and time in GMT. I expect if you work out your local timezone difference from GMT, you will see that it is the correct date.

Try creating an NSDateFormatter or NSCalendar with the appropriate locale and pass the date through that.

For further reading on this common topic, see this site written by another SO contributor.

Comments