一年中的日数如何发现?有没有一个简单的方法,我没有看到,或者我必须找到从1月1日到当前日期的秒数除以一天中的秒数?
解决方法
这是将
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
这是一个错误的做法,因为一天没有固定的秒数(从夏令时转换到夏令时间).
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。