一年中使用 iPhone SDK 的天数?

发布于 2024-08-05 22:05:04 字数 272 浏览 3 评论 0原文

我正在尝试获取当年的天数。

当我在 当前天数上尝试解决方案时使用 iPhone SDK 的月份?,并将 NSMonthCalendarUnit 替换为 NSYearCalendarUnit,我仍然可以获得该月的天数。

有谁知道我应该怎么做?

预先感谢您的帮助。

I'm trying to get the number of days in a current year.

When I try the solution on Number of days in the current month using iPhone SDK?, and replace NSMonthCalendarUnit by NSYearCalendarUnit, I still get the number of days for that month.

Does anyone know how I should do this?

Thanks in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

半衬遮猫 2024-08-12 22:05:04

这是 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

享受。

Here's a super accurate NSCalendar extension in Swift 2:

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)
    }
}

You can use it like this:

let calendar = NSCalendar.currentCalendar() // I'm using the Gregorian calendar
calendar.daysInYear()     // 365 (since it's currently 2015)
calendar.daysInYear(2016) // 366 (leap year!)

This is super flexible since we don't assume anything about the length of the calendar:

let hebrew = NSCalendar(calendarIdentifier: NSCalendarIdentifierHebrew)
hebrew?.daysInYear(-7)   // 354
hebrew?.daysInYear(-100) // 384

Enjoy.

伊面 2024-08-12 22:05:04

如果您只想使用公历,则可以手动计算。

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

If you're only going to use the Gregorian Calender, you can calculate it manually.

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
对你的占有欲 2024-08-12 22:05:04

我终于想出了一个可行的解决方案。我所做的是首先计算一年中的月份数,然后针对每个月计算该月的天数。

代码如下所示:

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;

它并不像我希望的那么整洁,但它适用于任何日历,例如普通公历日历或伊斯兰日历。

I finally came up with a solution that works. What I do is first calculate the number of months in the year and then for each month calculate the number of days for that month.

The code looks like this:

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;

It is not as neat as I would have hoped for but it will work for any calendar such as the ordinary gregorian one or the islamic one.

时光礼记 2024-08-12 22:05:04

使用 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;
}

Use the NSCalendar and NSDateComponent classes, like this:

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;
}
深海不蓝 2024-08-12 22:05:04

例如:

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,这导致(可能是)从零开始计数:)

As example:

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 
}

But default days return 355 and 354 this caused (may be) that counting begin from zero :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文