Number of days in given year using iPhone SDK?

时光毁灭记忆、已成空白 提交于 2019-12-23 08:30:10

问题


I'm trying to get the number of days in a current year.

When I try the solution on Number of days in the current month using iPhone SDK?, and replace NSMonthCalendarUnit by NSYearCalendarUnit, I still get the number of days for that month.

Does anyone know how I should do this?

Thanks in advance for your help.


回答1:


Here's a super accurate NSCalendar extension in Swift 2:

extension NSCalendar {
    func daysInYear(date: NSDate = NSDate()) -> Int? {
        let year = components([NSCalendarUnit.Year], fromDate: date).year
        return daysInYear(year)
    }

    func daysInYear(year: Int) -> Int? {
        guard let begin = lastDayOfYear(year - 1), end = lastDayOfYear(year) else { return nil }
        return components([NSCalendarUnit.Day], fromDate: begin, toDate: end, options: []).day
    }

    func lastDayOfYear(year: Int) -> NSDate? {
        let components = NSDateComponents()
        components.year = year
        guard let years = dateFromComponents(components) else { return nil }

        components.month = rangeOfUnit(NSCalendarUnit.Month, inUnit: NSCalendarUnit.Year, forDate: years).length
        guard let months = dateFromComponents(components) else { return nil }

        components.day = rangeOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.Month, forDate: months).length

        return dateFromComponents(components)
    }
}

You can use it like this:

let calendar = NSCalendar.currentCalendar() // I'm using the Gregorian calendar
calendar.daysInYear()     // 365 (since it's currently 2015)
calendar.daysInYear(2016) // 366 (leap year!)

This is super flexible since we don't assume anything about the length of the calendar:

let hebrew = NSCalendar(calendarIdentifier: NSCalendarIdentifierHebrew)
hebrew?.daysInYear(-7)   // 354
hebrew?.daysInYear(-100) // 384

Enjoy.




回答2:


If you're only going to use the Gregorian Calender, you can calculate it manually.

http://en.wikipedia.org/wiki/Leap_year#Algorithm

if year modulo 400 is 0 then leap
 else if year modulo 100 is 0 then no_leap
 else if year modulo 4 is 0 then leap
 else no_leap



回答3:


I finally came up with a solution that works. What I do is first calculate the number of months in the year and then for each month calculate the number of days for that month.

The code looks like this:

NSUInteger days = 0;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *today = [NSDate date];
NSDateComponents *components = [calendar components:NSYearCalendarUnit fromDate:today];
NSUInteger months = [calendar rangeOfUnit:NSMonthCalendarUnit
                                   inUnit:NSYearCalendarUnit
                                  forDate:today].length;
for (int i = 1; i <= months; i++) {
    components.month = i;
    NSDate *month = [calendar dateFromComponents:components];
    days += [calendar rangeOfUnit:NSDayCalendarUnit
                           inUnit:NSMonthCalendarUnit
                          forDate:month].length;
}

return days;

It is not as neat as I would have hoped for but it will work for any calendar such as the ordinary gregorian one or the islamic one.




回答4:


Use the NSCalendar and NSDateComponent classes, like this:

long GetDaysInYear(int year) {
    NSDateComponents* c = [[NSDateComponents alloc] init];
    c.year = year;
    NSCalendar* cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDate* startDate = [cal dateFromComponents:c];
    c.year += 1;
    NSDate* endDate = [cal dateFromComponents:c];
    return [cal components:NSDayCalendarUnit fromDate:startDate toDate:endDate options:0].day;
}



回答5:


As example:

func daysInYear(year: Int) -> Int {
   var calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
   var b = NSDate.dateWithNaturalLanguageString("01.01.\(year)", locale: NSLocale.currentLocale()) as! NSDate
   var e = NSDate.dateWithNaturalLanguageString("12.31.\(year)", locale: NSLocale.currentLocale()) as! NSDate

   return calendar!.components(NSCalendarUnit.CalendarUnitDay, fromDate: b, toDate: e, options: nil).day + 1 
}

But default days return 355 and 354 this caused (may be) that counting begin from zero :)



来源:https://stackoverflow.com/questions/1472386/number-of-days-in-given-year-using-iphone-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!