How might the day number of the year be found with swift? Is there a simple way that I'm not seeing, or do I have to find the number of seconds from Jan 1 to the current date and divide by the number of seconds in a day?
Asked
Active
Viewed 1.3k times
4 Answers
60
This is a translation of the answer to How do you calculate the day of the year for a specific date in Objective-C? to Swift.
Swift 2:
let date = NSDate() // now
let cal = NSCalendar.currentCalendar()
let day = cal.ordinalityOfUnit(.Day, inUnit: .Year, forDate: date)
print(day)
Swift 3:
let date = Date() // now
let cal = Calendar.current
let day = cal.ordinality(of: .day, in: .year, for: date)
print(day)
This gives 1
for the first day in the year, and 56 = 31 + 25
for today (Feb 25).
... or do I have to find the number of seconds from Jan 1 to the current date and divide by the number of seconds in a day
This would be a wrong approach, because a day does not have a fixed number of seconds (transition from or to Daylight Saving Time).
-
why does your method returns 56 mine 55 ? – Leo Dabus Feb 25 '15 at 07:13
-
@LeonardoSavioDabus: See updated answer: This method returns 1 for the first day in the year. – Martin R Feb 25 '15 at 07:13
-
NSCalendar.currentCalendar().ordinalityOfUnit(.CalendarUnitDay, inUnit: .CalendarUnitYear, forDate: NSDate() ) – Leo Dabus Feb 25 '15 at 07:14
-
perfect thank you, ordinalityOfUnit, no wonder I couldn't find it – Dan Oswalt Feb 25 '15 at 07:15
-
3@LeonardoSavioDabus: Yes of course (and you could write an extension for it, SCNR :) – I prefer multiple shorter statements in the answer, as they are better readable. – Martin R Feb 25 '15 at 07:15
-
when I run this I get 55, shouldn't i get 56? – Dan Oswalt Feb 25 '15 at 07:20
-
@zbnrg: I get 56 (and Leonardo as well, as it seems). – Martin R Feb 25 '15 at 07:21
-
@Martin R That's correct 55 using mine and 56 using yours – Leo Dabus Feb 25 '15 at 07:22
-
hm, okay, well it works for my purposes anyhow, thank you both – Dan Oswalt Feb 25 '15 at 07:23
-
Not working in Swift 2, compiler throws error: "Type of expression is ambiguous without more context" – ixany Sep 11 '15 at 19:16
-
@mrtn.lxo: Yes, the definition of calendar units changed with Swift 2. I have extended the answer for Swift 2, thanks for the feedback! – Martin R Sep 11 '15 at 19:20
15
Swift 3
extension Date {
var dayOfYear: Int {
return Calendar.current.ordinality(of: .day, in: .year, for: self)!
}
}
use like
Date().dayOfYear

Adam Smaka
- 5,977
- 3
- 50
- 55
4
Not at all !!! All you have to do is to use NSCalendar to help you do your calendar calculations as follow:
let firstDayOfTheYear = NSCalendar.currentCalendar().dateWithEra(1, year: NSCalendar.currentCalendar().component(.CalendarUnitYear, fromDate: NSDate()), month: 1, day: 1, hour: 0, minute: 0, second: 0, nanosecond: 0)! // "Jan 1, 2015, 12:00 AM"
let daysFromJanFirst = NSCalendar.currentCalendar().components(.CalendarUnitDay, fromDate: firstDayOfTheYear, toDate: NSDate(), options: nil).day // 55
let secondsFromJanFirst = NSCalendar.currentCalendar().components(.CalendarUnitSecond, fromDate: firstDayOfTheYear, toDate: NSDate(), options: nil).second // 4,770,357

Leo Dabus
- 229,809
- 59
- 489
- 571
0
You can find the number of days since your date like this:
let date = NSDate() // your date
let days = cal.ordinalityOfUnit(.CalendarUnitDay, inUnit: .CalendarUnitYear, forDate: date)
println(days)

Kanan Vora
- 224
- 1
- 9