Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem selecting date in UIDatePicker iphone

I´m using the UIDatePicker in a form but the problem is that when I select the date and time, the time in the text field is 5 hours after the time showed in the picker. I've read that there's a bug in date picker but I don't know how to solve this. I need to show the time of Mexico. I've tried doing this but nothing change.

datePicker.calendar = [NSCalendar autoupdatingCurrentCalendar];
datePicker.timeZone = [NSTimeZone localTimeZone];
datePicker.locale = [NSLocale currentLocale];

Can anyone help me with this??? XD

Thank you guys!!

like image 296
Rafael Jimeno Avatar asked May 02 '26 04:05

Rafael Jimeno


1 Answers

If you aren't using it already, I'd suggest doing:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterMediumStyle];
[df setTimeStyle:NSDateFormatterMediumStyle];

NSString *stringToDisplay = [df stringFromDate:myDateObject];

NSDateFormatter should take care of any time zone issues for you. You can read more here.

like image 66
Ben Mosher Avatar answered May 03 '26 22:05

Ben Mosher