Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDatePicker minimumdate

I'm confused with one question. I don't understand where I made mistake

I needed make minimumdate for my UIDatePicker near 1810 year. So I try make it like

    datePicker.minimumDate = [[NSDate alloc] initWithTimeIntervalSince1970: - (60 * 60 * 24 * 365 * 160)];

But I get wrong minimumdate: enter image description here

So I went the other way and solved the problem

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *comps = [NSDateComponents new];
comps.year = -160;
datePicker.minimumDate = [calendar dateByAddingComponents:comps toDate:[NSDate dateWithTimeIntervalSince1970:0] options:0];

But I can'nt understand why in the first case I get wrong minimumdate

like image 746
user2213271 Avatar asked Mar 16 '26 01:03

user2213271


1 Answers

datePicker.minimumDate = [[NSDate alloc] initWithTimeIntervalSince1970: - (60 * 60 * 24 * 365 * 160)];

160 Years are not 60 sec * 60 min * 24 hours * 365 days.

You did not take consideration of various factors, one of the easiest to know is leap year.

Edit:

Use 60.0 * 60 * 24 * 365 * 160 to make one of then float, so the result will be float as you may exceed the int32(ios) range.

like image 176
Anoop Vaidya Avatar answered Mar 18 '26 15:03

Anoop Vaidya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!