iphone - 在 Objective-C 中如何计算特定日期是一年中的哪一天?

标签 iphone objective-c nsdate nsdateformatter

这是我发现自己花了几个小时才弄清楚的事情,因此想与您分享。

问题是:如何确定特定日期是一年中的哪一天?

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;

其中 date 是您要确定一年中第几天的日期。关于 NSCalendar.ordinalityOfUnit 方法的文档是 here .

关于iphone - 在 Objective-C 中如何计算特定日期是一年中的哪一天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2080927/

相关文章:

ios - 在 XCode : Exception Breakpoints 中调试

ios - UIScrollView-zoomToRect :animated: and CGPoint conversion between views

iphone - iOS - 如何不在屏幕上显示导航栏?

iphone - 在tableviewcontroller的编辑模式下重命名 "Delete"按钮

Swift 每 x 天创建本地通知

nsstring - 从服务器接收损坏的信息

快速检查 NSDate 不为零

ios - Swift 中的零宽度空间?

ios - 在 GMAP iOS 中获取多个位置的路线。

ios - XMPP IOS Openfire 在发送元素后断开连接