一年中使用 iPhone SDK 的天数?
我正在尝试获取当年的天数。
当我在 当前天数上尝试解决方案时使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是 Swift 2 中的一个超级准确的
NSCalendar
扩展:您可以像这样使用它:
这是超级灵活的,因为我们不对日历的长度做出任何假设:
享受。
Here's a super accurate
NSCalendar
extension in Swift 2:You can use it like this:
This is super flexible since we don't assume anything about the length of the calendar:
Enjoy.
如果您只想使用公历,则可以手动计算。
http://en.wikipedia.org/wiki/Leap_year#Algorithm
If you're only going to use the Gregorian Calender, you can calculate it manually.
http://en.wikipedia.org/wiki/Leap_year#Algorithm
我终于想出了一个可行的解决方案。我所做的是首先计算一年中的月份数,然后针对每个月计算该月的天数。
代码如下所示:
它并不像我希望的那么整洁,但它适用于任何日历,例如普通公历日历或伊斯兰日历。
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:
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.
使用 NSCalendar 和 NSDateComponent 类,如下所示:
Use the
NSCalendar
andNSDateComponent
classes, like this:例如:
但默认日期返回 355 和 354,这导致(可能是)从零开始计数:)
As example:
But default days return 355 and 354 this caused (may be) that counting begin from zero :)