UIDatePicker returns wrong date (-1 day to the real date)
Asked Answered
P

10

13

I have a UIDatePicker mm/dd/yy. It works fine, but there is one problem: I set minimum and maximum date to it, and when user tries to choose the forbidden day, month or year, the [datePicker date] property begins working wrong. It returns you the current day - 1 or current month - 1 or current year - 1. I added some pictures, so you can see the situation.

Correct date This is correct
wrong date This is wrong (After choosing the forbidden date)

Does somebody know, how can I fix this ? Thanks !

UPD: Code

[self.myDatePicker setMinimumDate:[NSDate date]];
[self.myDatePicker setMaximumDate:[[NSDate date] addTimeInterval:2 * 365.25 * 24 * 60 * 60]]; // to get upto 5 years
NSDate * now = [[NSDate alloc] init];
[self.myDatePicker setDate: now animated: YES];

self.myDatePicker.timeZone = [NSTimeZone localTimeZone];
self.myDatePicker.calendar = [NSCalendar currentCalendar];
Parliamentarian answered 7/1, 2013 at 17:11 Comment(6)
How should we be able to help you without any relevant code?Marshallmarshallese
@vikingosegundo, well, its classic here. Anyway, please see my update answer.Parliamentarian
addTimeInterval: isnt classic. you shouldnt do date calculation like that. please watch WWDC 2011 Video "Session 117 - Performing Calendar Calculations"Marshallmarshallese
@Marshallmarshallese Ok, thanks. But it doesn't change the situation anyway.Parliamentarian
@Marshallmarshallese I am in Austria right now (holidays). You think its timezone ?Parliamentarian
Someone else was having similar problems with a Mideastern locale. Didn't appear to be timezone -- more like the calendar wasn't Gregorian, but when calendar type was printed it claimed to be.Pharmaceutical
M
5

It is deffinately something with the timezones and/or Daylight Saving Times. But it must be very subtle, as the code looks fine (beside the interval). Now to my question about if you are in russia:

This year the Kremlin did several back and forth swings on keeping daylight saving times forever. Actually I am not sure, what they decided at last. But maybe it isnt reflected correctly in Cocoa. The the video WWDC 2011 Video "Session 117 - Performing Calendar Calculations" , the presenter even mentions that things like that can happen.

Please try to work with dates with manually set times to noon, as this would keep you out of such mess.


The world just saw a similar misbehavior in iOS 6: the DND-Always-Active bug. I bet this was for a wrong date format (YYYY instead of yyyy)


Also try to set the timezone property on the picker at the very first thing and assign a manually instantiated Gregorian calendar to it.

Marshallmarshallese answered 7/1, 2013 at 17:46 Comment(1)
Yeah, it was a timezone problem. I changed timezone to Californian and it worked great. So now, I have to know find out, how to fix Russian timezone issue. Thanks for help.Parliamentarian
T
15

Just add one line of code for setting your timezone.

self.datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];

0 is for GMT 00 . Add according to your time zone.

Then answered 9/11, 2016 at 16:9 Comment(2)
this should be the right answer, since it actually addressed (and solves) the core problem instead of attempting to make hacky patches. Thank you, you solved a major issue for me.Overshine
I deleted about 6-7 lines of code for this simple solution that solved the problem.Gawain
S
6

My solution was to set the returned date to 12:00 AM as NSDates work in UTC

NSDate * adjustedDate = [[NSCalendar currentCalendar] dateBySettingHour:12 minute:0 second:0 ofDate:sender.date options:0];

Also for date calculations you shoud use NSCalender methods and not addTimeInterval

Slacken answered 30/10, 2014 at 13:8 Comment(0)
S
5

Check if you use the wrong formatting symbols with big letters: "YYYY". Replace them with "yyyy".

Spectrohelioscope answered 7/1, 2013 at 17:21 Comment(0)
M
5

It is deffinately something with the timezones and/or Daylight Saving Times. But it must be very subtle, as the code looks fine (beside the interval). Now to my question about if you are in russia:

This year the Kremlin did several back and forth swings on keeping daylight saving times forever. Actually I am not sure, what they decided at last. But maybe it isnt reflected correctly in Cocoa. The the video WWDC 2011 Video "Session 117 - Performing Calendar Calculations" , the presenter even mentions that things like that can happen.

Please try to work with dates with manually set times to noon, as this would keep you out of such mess.


The world just saw a similar misbehavior in iOS 6: the DND-Always-Active bug. I bet this was for a wrong date format (YYYY instead of yyyy)


Also try to set the timezone property on the picker at the very first thing and assign a manually instantiated Gregorian calendar to it.

Marshallmarshallese answered 7/1, 2013 at 17:46 Comment(1)
Yeah, it was a timezone problem. I changed timezone to Californian and it worked great. So now, I have to know find out, how to fix Russian timezone issue. Thanks for help.Parliamentarian
M
1

I ran into the same trouble and this is what i derived:

Don't use [date description] to check NSDate if you want correct representation for your system. Use NSDateFormatter, because it shows date based on your system preferences (in simulator it will be simulators preferences).

For example:

NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateStyle:NSDateFormatterShortStyle];
[df setTimeStyle:NSDateFormatterNoStyle];
NSLog(@"date for locale is %@", [df stringFromDate:date]);
Menstruum answered 31/3, 2013 at 10:11 Comment(0)
C
1

Try this,

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.string(from: yourPicker.date)
Capp answered 3/11, 2020 at 7:38 Comment(0)
A
0

Might be due to TimeZone...

Set your time zone.

Arcadia answered 7/1, 2013 at 17:15 Comment(2)
I did this: self.myDatePicker.timeZone = [NSTimeZone localTimeZone]; self.myDatePicker.calendar = [NSCalendar currentCalendar]; Doesn't help.Parliamentarian
I did set the TimeZone it didn't help me eitherElfrieda
O
0

For me this problem is resolved by adding this line

datePicker.date.addingTimeInterval(TimeInterval(TimeZone.current.secondsFromGMT()))

This line adds up the seconds in dateTime of UIDatePicker and dateTime will be equal to current dateTime

Overgrow answered 25/6, 2022 at 6:42 Comment(0)
V
-1
Boolean futureevent;
- (void)viewDidLoad
{
 futureevent = false;
}

int intervall = (int) [currentdate timeIntervalSinceDate: datePicker.date] / 60;

if (intervall < 1200 && intervall > 0)
{
    futureevent = true;
    NSDate *newDate1 = [datePicker.date dateByAddingTimeInterval:60*60*24*1];
    birthdate = [newDate1.description substringToIndex:spaceRange.location];
}
else
{
    if (futureevent)
    {
        NSDate *newDate1 = [datePicker.date dateByAddingTimeInterval:60*60*24*1];
        birthdate = [newDate1.description substringToIndex:spaceRange.location];
    }
    else
    {
        birthdate = [datePicker.date.description substringToIndex:spaceRange.location];
    }
}
Vibrato answered 23/6, 2013 at 11:34 Comment(0)
T
-2

It does not return wrong date. What actually happens is, when you select a so called forbidden date, the date picker gets reset to maximum or minimum allowed date with first moment of the day i.e 12:00AM.

So if you are at a place where time zone is for example, 2 hours ahead of GMT, the date returned by date picker will be yesterday's 10:00PM GMT. So here, you might think it is returning yesterday's date but if you convert it to your time zone, you will get today's date only but time component will be 12:00AM.

Transvestite answered 13/6, 2019 at 5:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.