Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 dateThis is correct
wrong dateThis 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];
like image 483
SmartTree Avatar asked Jan 07 '13 17:01

SmartTree


2 Answers

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.

like image 200
Arundev Avatar answered Sep 19 '22 12:09

Arundev


Try this,

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
formatter.string(from: yourPicker.date)
like image 28
Wimukthi Rajapaksha Avatar answered Sep 17 '22 12:09

Wimukthi Rajapaksha