I have an NSDatePicker in my nib file. In code, I set it's timezone to be the GMT calendar's timezone:
[datePicker setTimeZone:[calendar timeZone]];
I only really care about the time (hours, minutes) on the datepicker which I populate programmatically:
NSDate *anyDate = [NSDate date];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:anyDate];
[components setHour:hours];
[components setMinute:minutes];
NSDate *newDate = [calendar dateFromComponents:components];
[datePicker setDateValue:newDate];
This correctly has the desired effect of setting the date pickers time to the time I want. So if hours is 8 and minutes is 30, the date picker shows 8:30. However, if I type an 8 into the hour field of the date picker it displays as a 3. Something weird is going on with timezones somewhere but I'm not sure where...
Seems like I'm not the first person to stumble across this issue. The solution, for those who are interested, I found here http://www.cocoabuilder.com/archive/cocoa/305254-nsdatepicker-weirdness-with-time.html. Apparently if you set the timezone of a date picker but not the calendar, you get this issue. The resolution is this:
[datePicker setTimeZone:[calendar timeZone]];
[datePicker setCalendar:calendar];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With