How to get current year from NSDate
and how to find out whether that year is leap year or not in Objective-C?
You can do the following:
- (BOOL)isYearLeapYear:(NSDate *) aDate {
NSInteger year = [self yearFromDate:aDate];
return (( year%100 != 0) && (year%4 == 0)) || year%400 == 0;
}
- (NSInteger)yearFromDate:(NSDate *)aDate {
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.dateFormat = @"yyyy";
NSInteger year = [[dateFormatter stringFromDate:aDate] integerValue];
return year;
}
Wikipedia:
if year is divisible by 400 then
is_leap_year
else if year is divisible by 100 then
not_leap_year
else if year is divisible by 4 then
is_leap_year
else
not_leap_year
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With