Swifterswift: Add Date.numberOfDaysInYear

Created on 25 Aug 2018  ·  2Comments  ·  Source: SwifterSwift/SwifterSwift

Please fill out this template when filing an issue.

All comment lines beginning with an > instruct you with what info we expect. You can delete those lines once you've filled in the info.

[x] I've read, understood, and done my best to follow the Contributing guidelines before opening this issue.

What did you do?

Provide a compute property.

What did you expect to happen?

Get days in year for Date.

What happened instead?

Please replace this with of what happened instead.

SwifterSwift Environment

  • SwifterSwift version: 4.4.0
  • Xcode version: 9.4.1
  • macOS version running Xcode: 10.13.6
  • Swift version: 4.1.2
  • Platform(s) running SwifterSwift: macOS

Demo Project

extension Date {
    var numberOfDaysInYear: Int {
        return Calendar.current.ordinality(of: .day, in: .year, for: self)!
    }
}
let now = Date()

dump(now)
print(now.numberOfDaysInYear)
▿ 2018-08-25 08:43:25 +0000
  - timeIntervalSinceReferenceDate: 556879405.01351404
237
feature request good first issue help wanted

Most helpful comment

Actually the code example is dayInYear. To get numberOfDaysLeftInYear it would be calculated as such:

extension Date {
    var dayInYear: Int {
        return Calendar.current.ordinality(of: .day, in: .year, for: self)!
    }

    var numberOfDaysInYear: Int {
        return Calendar.current.range(of: .day, in: .year, for: self)!.upperBound - 1
    }

    var numberOfDaysLeftInYear: Int {
        return numberOfDaysInYear - dayInYear
    }
}

Date().numberOfDaysLeftInYear // 81 (for Oct 11, 2018)

All 2 comments

imho: name should be numberOfDaysLeftInYear or something like it.
because number-of-days-in-year is either 365 or 366 depending on leap-year

Actually the code example is dayInYear. To get numberOfDaysLeftInYear it would be calculated as such:

extension Date {
    var dayInYear: Int {
        return Calendar.current.ordinality(of: .day, in: .year, for: self)!
    }

    var numberOfDaysInYear: Int {
        return Calendar.current.range(of: .day, in: .year, for: self)!.upperBound - 1
    }

    var numberOfDaysLeftInYear: Int {
        return numberOfDaysInYear - dayInYear
    }
}

Date().numberOfDaysLeftInYear // 81 (for Oct 11, 2018)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

SD10 picture SD10  ·  3Comments

omaralbeik picture omaralbeik  ·  3Comments

AYastrebov picture AYastrebov  ·  3Comments

omaralbeik picture omaralbeik  ·  3Comments

omaralbeik picture omaralbeik  ·  3Comments