Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out whether current year leap year or not in iphone [duplicate]

How to get current year from NSDate and how to find out whether that year is leap year or not in Objective-C?

like image 674
Ashok Avatar asked May 29 '13 08:05

Ashok


2 Answers

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;
}
like image 81
Groot Avatar answered Oct 13 '22 12:10

Groot


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
like image 27
Michaël Avatar answered Oct 13 '22 10:10

Michaël