我试图确定回到19C的应用程序在两个不同日期之间的闰年天数 - 这里是一个方法示例:计算闰年在xcode中的时间差的天数
-(NSInteger)leapYearDaysWithinEraFromDate:(NSDate *) startingDate toDate:(NSDate *) endingDate {
// this is for testing - it will be changed to a datepicker object
NSDateComponents *startDateComp = [[NSDateComponents alloc] init];
[startDateComp setSecond:1];
[startDateComp setMinute:0];
[startDateComp setHour:1];
[startDateComp setDay:14];
[startDateComp setMonth:4];
[startDateComp setYear:2005];
NSCalendar *GregorianCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//startDate declared in .h//
startDate = [GregorianCal dateFromComponents:startDateComp];
NSLog(@"This program's start date is %@", startDate);
NSDate *today = [NSDate date];
NSUInteger unitFlags = NSDayCalendarUnit;
NSDateComponents *temporalDays = [GregorianCal components:unitFlags fromDate:startDate toDate:today options:0];
NSInteger days = [temporalDays day];
// then i will need code for the number of leap year Days
return 0;//will return the number of 2/29 days
}
所以我有日期之间的总天数。现在我需要减去闰年的日子?
PS - 我知道两闰年的天也有在这个例子中,但应用程序将回到19世纪...
如果结束年份是闰年,则会关闭一次。测试应该是currentYear <=(startYear + numYears)。 – Mark
@RoshDamunki 1900年不是闰年。检查维基百科,可以被100除尽但不是400的年份不是闰年。 –
是的,我发布后我才看到。它现在看起来工作得很好。谢谢你谢谢 – RoshDamunki