如何计算Objective-C中特定日期的一年中的哪一天? [英] How do you calculate the day of the year for a specific date in Objective-C?
本文介绍了如何计算Objective-C中特定日期的一年中的哪一天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现自己花了好几个小时才想出来,因此想和你分享。
This is something I found myself spending hours to figure out and therefore want to share with you.
问题是:我如何确定一年中的哪一天特定日期?
The question was: How do I determine the day of the year for a specific date?
例如1月15日是第15天,12月31日是不是闰年的第365天。
e.g. January 15 is the 15th day and December 31 is the 365th day when it's not leap year.
推荐答案
试试这个:
NSCalendar *gregorian =
[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger dayOfYear =
[gregorian ordinalityOfUnit:NSDayCalendarUnit
inUnit:NSYearCalendarUnit forDate:[NSDate date]];
[gregorian release];
return dayOfYear;
其中日期
是您想要的日期确定一年中的某一天。 NSCalendar.ordinalityOfUnit方法的文档是 此处 。
where date
is the date you want to determine the day of the year for. The documentation on the NSCalendar.ordinalityOfUnit method is here.
这篇关于如何计算Objective-C中特定日期的一年中的哪一天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文