查找当前时间是否在某个范围内
我可以找到如何获取日期是否在范围之间,但我似乎无法在特定时间创建日期。
查看 [NSDate date] 是否在时间范围内的最简单方法是什么?
我想显示如下的个性化问候语:
中午 12 点 - 下午 4:59:9999 @“下午好,foo”
下午 5 点 - 晚上 11:59:9999 @“晚上好,foo”
上午 12 点 - 上午 11:59:9999 @“早上好,foo”
I can find how to get if a date is between a range, but I cant seem how to create a date at a specific time.
What would be the simplest way to see if [NSDate date] is between a time range?
I want to display a personalized greeting like the following:
12 pm - 4:59:9999 pm @"Good afternoon, foo"
5 pm - 11:59:9999 pm @"Good evening, foo"
12 am - 11:59:9999 am @"Good morning, foo"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,您可以使用
NSDateComponents
它将返回 24 小时格式的小时。斯威夫特3
Yes you can using
NSDateComponents
which will return the hour in the 24 hour format.Swift 3
更新为 Swift 3.0
Updated for Swift 3.0
使用
NSCalendar
实例从您的NSDate
创建一个NSDateComponents
实例,然后只需检查的小时、分钟和秒属性NSDateComponents
并呈现适当的消息。Use an
NSCalendar
instance to create aNSDateComponents
instance out of yourNSDate
, then just check the hours, minutes and seconds property of theNSDateComponents
and present the appropriate message.中午是12:00 PM。 下午是从12:01 PM到5:00 PM左右。 晚上是从下午 5:01 到晚上 8 点,或日落前后。 夜晚是从日落到日出,即从下午8:01到上午5:59。
Noon is at 12:00 PM. Afternoon is from 12:01 PM to around 5:00 PM. Evening is from 5:01 PM to 8 PM, or around sunset. Night is from sunset to sunrise, so from 8:01 PM until 5:59 AM.
您可以使用类
NSDate
来获取 iPhone 上的时间。参考:NSDate 文档
You could just use the class
NSDate
to grab the time on the iPhone.Reference: NSDate Documentation