如何使用 NSDate 从今天的日期查找工作日?
我知道我可以通过 [NSDate date];
找出今天的日期,但是我如何找出今天是一周中的哪一天,例如星期六、星期五等。
我知道 %w
code> 可以在 NSDateFormatter
中使用,但我不知道如何使用它。
I know that I can figure out today's date by [NSDate date];
but how would I find out today day of the week, like Saturday, Friday etc.
I know that %w
can be used in NSDateFormatter
, but I don't know to use it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您只需要工作日而不需要其他组件(需要 iOS 8+@HuguesBR 的回答>)。
(如果您没有得到正确答案,请检查您是否将
NSCalendarUnitWeekday
与其他与周相关的组件(如NSCalendarUnitWeekdayOrdinal)输入错误>
等)Swift 3:
See @HuguesBR's answer if you just need the weekday without other components (requires iOS 8+).
(If you don't get a correct answer, check if you have mistyped
NSCalendarUnitWeekday
with other week-related components likeNSCalendarUnitWeekdayOrdinal
, etc.)Swift 3:
如果有人对 Swift 解决方案感兴趣,请使用:
请参阅演示。
更新:已修复以在 Swift 3 中工作
If someone is interested in a Swift solution, then use:
See the demo.
Update: fixed to work in Swift 3
这是适用于 iOS 8 的更新版本
This is updated version valid for iOS 8
较短的版本
Shorter version