Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

UIDatePicker bug in iOS 4

zaplitnyzaplitny Posts: 17Registered Users
edited January 2011 in iOS SDK Development
I've found very strange bug of UIDatePicker in iOS4.

It seems that UIDatePicker timeZone is set during project build or something like this. You can reproduce it by setting
NSLog(@"Date : %@", [[NSDate date] description]);
datePicker.date = [NSDate date];

If you build it and run first time - all is ok. Then change your timezone to another and run it again (without cleaning targets) - in log you'll see correct date (according to new time zone) but in datePicker you'll see other date (from prev time zone). If you clean targets and rebuild it all is ok.

The workaround for this is to set current timezone to datePicker:
datePicker.timeZone = [NSTimeZone localTimeZone];

It's very strange behavior
Post edited by zaplitny on
<a href="http://www.aloggers.com"; target="_blank">aTimeLogger</a> - application for tracking your everyday activities

Replies

  • brendandbrendand Posts: 223Registered Users
    edited July 2010
    I also had this problem with my app. I've just applied this fix to it and it works great. Thanks for posting the solution.

    zaplitny wrote: »
    I've found very strange bug of UIDatePicker in iOS4.

    It seems that UIDatePicker timeZone is set during project build or something like this. You can reproduce it by setting
    NSLog(@"Date : %@", [[NSDate date] description]);
    datePicker.date = [NSDate date];
    

    If you build it and run first time - all is ok. Then change your timezone to another and run it again (without cleaning targets) - in log you'll see correct date (according to new time zone) but in datePicker you'll see other date (from prev time zone). If you clean targets and rebuild it all is ok.

    The workaround for this is to set current timezone to datePicker:
    datePicker.timeZone = [NSTimeZone localTimeZone];
    

    It's very strange behavior
  • stevetrollopestevetrollope Posts: 14Registered Users
    edited July 2010
    I also had this problem since building my latest update binaries with the new SDK 4.
    Interface Builder doesn't even have a field to set the time zone of the UIDatePicker so it has to be done in ViewDidLoad as suggested.
    The documents say that leaving it nil and not setting a value would have the same behaviour as
    datePicker.timezone = [NSTimeZone localtimezone];
    but it definitely doesn't.
    Not very helpful of Apple to change the default behaviour of this commonly used object. There must be a lot of app developers hit by this one and now receiving emails from users of their apps in different time zones complaining about their pickers displaying incorrect values.
    Any one else out there had issues?:mad:
  • alku83alku83 Posts: 6New Users
    edited July 2010
    I'm also seeing this issue. Frustrating!
  • cribasoftcribasoft Posts: 159Registered Users
    edited July 2010
    My HoursTracker apps ran into this with the 2.0 (iOS 4) update. I got probably 500 emails about it from users who were very frustrated, and some 1 star reviews.

    Apple rushed a 2.0.1 update that includes setting the timezone (and locale) to the current ones. That works around the problem.

    I created a simple sample project that demonstrates the problem and submitted it to Apple as a iOS 4 bug.

    Note that this bug only comes up if you use a .xib -- the picker behaves properly if you add it in code.
  • dobrydobry Posts: 295Registered Users @ @
    edited July 2010
    I'm encountering similar problem on simulator iOS 4.0 (SDK 4 final) without IB and it hasn't to do with timezone change after build (I deleted build, cleaned targets and made rebuild).
    NSLog(@"birthday=%@",birthdayDate);
      NSLog(@"now     =%@",[NSDate date]);
      [datePicker setDate:bbirthdayDate animated:NO];   
      datePicker.timeZone = [NSTimeZone localTimeZone];
      datePicker.locale   = [NSLocale currentLocale]; 
      datePicker.calendar = [NSCalendar currentCalendar];
      datePicker.datePickerMode = UIDatePickerModeDate;
      [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    NSLog diplays correctly this:
    birthday=2000-07-16 12:00:00 +0300
    now     =2010-07-14 19:30:46 +0300
    
    But DatePicker displays 2000-07-15. However on iPad simulator (3.2) and iPhone 3.1.2 it works fine. Seems to be a problem on iOS4. It didn't help to set localTimeZone. Btw - my MacBook and simulator is working with timezone of Ukraine (Kiev). I don't have a device with iOS4 to test it.
  • zaplitnyzaplitny Posts: 17Registered Users
    edited July 2010
    dobry wrote: »
    I'm encountering similar problem on simulator iOS 4.0 (SDK 4 final) without IB and it hasn't to do with timezone change after build (I deleted build, cleaned targets and made rebuild).
    NSLog(@"birthday=%@",birthdayDate);
      NSLog(@"now     =%@",[NSDate date]);
      [datePicker setDate:bbirthdayDate animated:NO];   
      datePicker.timeZone = [NSTimeZone localTimeZone];
      datePicker.locale   = [NSLocale currentLocale]; 
      datePicker.calendar = [NSCalendar currentCalendar];
      datePicker.datePickerMode = UIDatePickerModeDate;
      [datePicker addTarget:self action:@selector(datePickerValueChanged:) forControlEvents:UIControlEventValueChanged];
    
    NSLog diplays correctly this:
    birthday=2000-07-16 12:00:00 +0300
    now     =2010-07-14 19:30:46 +0300
    
    But DatePicker displays 2000-07-15. However on iPad simulator (3.2) and iPhone 3.1.2 it works fine. Seems to be a problem on iOS4. It didn't help to set localTimeZone. Btw - my MacBook and simulator is working with timezone of Ukraine (Kiev). I don't have a device with iOS4 to test it.

    I reproduced it too, but I think it's other type of bug that is connected with mode of picker. If you change date picker mode to UIDatePickerModeDateAndTime it will show correct value
    <a href="http://www.aloggers.com"; target="_blank">aTimeLogger</a> - application for tracking your everyday activities
  • 2ndSegment2ndSegment Posts: 5New Users
    edited July 2010
    Dobry,

    Thanks for starting this thread. I've been challenged by this as well. Since my app is frequently used for tracking time across time zones, this is a big problem for me. In viewWillAppear:
    datePicker.timeZone = [NSTimeZone localTimeZone];
    

    Setting localTime Zone improved the situation, but the problem cropped up again. I played around with a number of things and I get the most consistent results with:
    datePicker.timeZone = [NSTimeZone defaultTimeZone];
    

    Although Today (as displayed in blue on the picker) is not always correct, the correct date is displayed when the time zone changes. Quitting the app and starting up again (iOS 4) always restores the correct behavior to the datePicker. So this problem is only a problem for me because the app is "freeze dried", the time zone changes, the app is brought to the foreground and the datePicker doesn't update for the new localTimeZone.

    John
  • bharath2020bharath2020 Posts: 151Registered Users
    edited July 2010
    Thanks Zaplitny..

    It Saved my Time :-)
    Bharath<br />
    iOS Blog : <a href="http://iphone2020.wordpress.com"; target="_blank">http://iphone2020.wordpress.com</a>;
  • dobrydobry Posts: 295Registered Users @ @
    edited September 2010
    I solved the problem I mentioned by setting datePickerMode before the date.
  • adarshraoadarshrao Posts: 11Registered Users
    edited October 2010
    please post some code asap.
  • tahir.fariditahir.faridi Posts: 1New Users
    edited December 2010
    Hi All,
    whatever my experiences say that the following code is more stable solution of the following problem, it picks your device time zone and even format(12 hrs/24 hrs)
    datePicker.timeZone = [NSTimeZone systemTimeZone];
    


    Note : if some one has better solution, let me know on tahir.faridi@halosys.com

    Thanks
    Tahir Faridi
  • deenna_vargilzdeenna_vargilz Posts: 1New Users
    edited January 2011
    Hi all,

    I have same problem about UIDatePicker in iOS SDK 4.2, but it seem just for iPad Simulator 4.2. So the date that I choose in UIDatePicker become one day before. But it become normal if I make "pickerDate.datePickerMode = UIDatePickerModeDateAndTime". My apps just need for "UIDatePickerModeDate". Anyone can help me?

    If anyone has a solution, please tell me in deenna_vargilz@yahoo.com


    Thanks

    Dina
Sign In or Register to comment.