user2213271
user2213271

Reputation: 421

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

Upvotes: 3

Views: 281

Answers (2)

Natan R.
Natan R.

Reputation: 5191

In my opinion, this has to do with the maximum value of an int. Making this multiplication results in 5.045.760.000, and as you did it a multiplication of whole numbers and not 60.0 * 60.0 maybe this is the reason it's failing.

Try to put directly the number I posted here, or just declare it as a float and pass in to the method, or make one of them float so the result will be a float: - 60.0 * 60 * 24 * 365 * 160

Upvotes: 1

Anoop Vaidya
Anoop Vaidya

Reputation: 46563

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.

Upvotes: 4

Related Questions