nicoko
nicoko

Reputation: 195

Number of days in given year using iPhone SDK?

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.

Upvotes: 7

Views: 2976

Answers (5)

Sam Soffes
Sam Soffes

Reputation: 14935

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.

Upvotes: 8

iTux
iTux

Reputation: 2096

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 :)

Upvotes: 0

Krumelur
Krumelur

Reputation: 32507

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;
}

Upvotes: 1

Godisemo
Godisemo

Reputation: 1813

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.

Upvotes: 5

kubi
kubi

Reputation: 49364

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

Upvotes: 5

Related Questions