我正在努力获得当年的天数。
当我在Number of days in the current month using iPhone SDK?上尝试解决方案,并用NSYearCalendarUnit替换NSMonthCalendarUnit时,我仍然得到该月的天数。
有谁知道我应该怎么做?
提前感谢您的帮助。
答案 0 :(得分:8)
这是Swift 2中超级准确的NSCalendar
扩展名:
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)
}
}
你可以像这样使用它:
let calendar = NSCalendar.currentCalendar() // I'm using the Gregorian calendar
calendar.daysInYear() // 365 (since it's currently 2015)
calendar.daysInYear(2016) // 366 (leap year!)
这是超级灵活的,因为我们不假设日历的长度:
let hebrew = NSCalendar(calendarIdentifier: NSCalendarIdentifierHebrew)
hebrew?.daysInYear(-7) // 354
hebrew?.daysInYear(-100) // 384
享受。
答案 1 :(得分:5)
如果您只打算使用公历,您可以手动计算。
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
答案 2 :(得分:5)
我终于想出了一个有效的解决方案。我所做的是先计算一年中的月数,然后计算每个月的天数。
代码如下所示:
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;
它并不像我希望的那样整洁,但它适用于任何日历,例如普通的格里高利或伊斯兰日历。
答案 3 :(得分:1)
使用NSCalendar
和NSDateComponent
类,如下所示:
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;
}
答案 4 :(得分:0)
例如:
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
}
但默认天数返回355和354,这导致(可能)计数从零开始:)