How to find out whether the current year is a leap year or not on iphone

How to get the current year with NSDate and how to find out if a year is a leap year or not in Objective-C?

+4
source share
4 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; } 
+14
source

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 
+4
source

first you need to find the year using NSDateComponant, and if you get the year, divide it by 4, if you divide, then this is a leap year.

+1
source
  NSDate *date = [NSDate date]; NSLog(@"dat eis %@",date); NSDateFormatter *dateF = [[NSDateFormatter alloc]init]; [dateF setDateFormat:@"YYYY"]; NSString *dateS = [dateF stringFromDate:date ]; NSLog(@"date is %@",dateS); int myInt = [dateS intValue]; if(myInt%4== 0) { NSLog(@"Its a leap year "); } else{ NSLog(@"Not A leap year"); } 
+1
source

Source: https://habr.com/ru/post/1483309/


All Articles