解决方法
这是将
How do you calculate the day of the year for a specific date in Objective-C?的答案翻译成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)
今年第一天为1,今天(二月二十五日)为56 = 31 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