【问题标题】:Count the number of Leap Year Days in a temporal difference in xcode计算xcode中时间差异中的闰年天数
【发布时间】:2012-05-16 18:44:18
【问题描述】:

我正在尝试确定可追溯到 19 世纪的应用程序的两个不同日期之间的闰年天数 - 这是一个方法示例:

-(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 世纪...

【问题讨论】:

    标签: objective-c ios5 xcode4.2


    【解决方案1】:

    对于斯威夫特

    func getLeapCount(startDate : NSDate , endDate : NSDate)-> Int{
        var intialDate = startDate
        var dateComponent = NSDateComponents()
        dateComponent.day = 1
        var leapCount = 0;
        var currentCalendar = NSCalendar.currentCalendar()
            while (intialDate.compare(endDate) == NSComparisonResult.OrderedAscending) {
                intialDate = currentCalendar.dateByAddingComponents(dateComponent, toDate: intialDate, options: NSCalendarOptions.allZeros)!            
                if self.isLeapYear(startDate){
                    ++leapCount
                }
        }
    
        return leapCount
    }
    func isLeapYear (year : NSDate )-> Bool{
        let cal = NSCalendar.currentCalendar()
        let year = cal.component(NSCalendarUnit.CalendarUnitYear, fromDate: year)
        return (( year%100 != 0) && (year%4 == 0)) || year%400 == 0;
    }
    

    【讨论】:

    • 我直接用了代码,因为我用的是Swift,但提问者是Objc程序员。
    【解决方案2】:

    一个简单的解决方案是遍历两个日期之间的所有年份,如果是闰年,则调用函数递增计数器。 (来自维基百科)

    if year modulo 400 is 0 then 
       is_leap_year
    else if year modulo 100 is 0 then 
       not_leap_year
    else if year modulo 4 is 0 then 
       is_leap_year
    else
       not_leap_year
    

    这将为您提供闰年的数量,以及您需要减去的闰年天数。 可能有更有效的方法,但这是我目前能想到的最简单的方法。

    【讨论】:

    • 至少你可以给出 Objc 代码示例。
    【解决方案3】:

    好吧,好吧,你把这件事弄得太复杂了。我想这就是你想要的:

    NSUInteger leapYearsInTimeFrame(NSDate *startDate, NSDate *endDate)
    {
        // check to see if it's possible for a leap year (e.g. endDate - startDate > 1 year)
        if ([endDate timeIntervalSinceDate:startDate] < 31556926)
            return 0;
    
        // now we go year by year
        NSUInteger leapYears = 0;
        NSUInteger startYear = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:startDate].year;
        NSUInteger numYears = [[NSCalendar currentCalendar] components:NSYearCalendarUnit fromDate:endDate].year - startYear;
    
        for (NSUInteger currentYear = startYear; currentYear <= (startYear + numYears); currentYear++) {
            if (currentYear % 400 == 0)
                // divisible by 400 is a leap year
                leapYears++;
            else if (currentYear % 100 == 0)
                /* not a leap year, divisible by 100 but not 400 isn't a leap year */ 
                continue;
            else if (currentYear % 4 == 0)
                // divisible by 4, and not by 100 is a leap year
                leapYears++;
            else 
                /* not a leap year, undivisble by 4 */
                continue;
        }
    
        return leapYears;    
    }
    

    【讨论】:

    • 如果结束年份是闰年,这个值会减一。测试应该是 currentYear
    • @RoshDamunki 1900 不是闰年。检查维基百科,能被 100 整除但不能被 400 整除的年份不是闰年。
    • 是的,我在发布后确实看到了。它现在似乎工作得很好。谢谢谢谢
    猜你喜欢
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多