Search code examples
iphonetimezoneuidatepickerhour

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


Solution

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