查找当前时间是否在某个范围内

发布于 2024-11-27 17:27:47 字数 261 浏览 1 评论 0原文

我可以找到如何获取日期是否在范围之间,但我似乎无法在特定时间创建日期。

查看 [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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

宛菡 2024-12-04 17:27:47

是的,您可以使用 NSDateComponents 它将返回 24 小时格式的小时。

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:[NSDate date]];
NSInteger hour = [components hour];

if(hour >= 0 && hour < 12)
    NSLog(@"Good morning, foo");
else if(hour >= 12 && hour < 17)
    NSLog(@"Good afternoon, foo");
else if(hour >= 17)
    NSLog(@"Good evening, foo");

斯威夫特3

let hour = Calendar.current.component(.hour, from: Date())

if hour >= 0 && hour < 12 {
    print("Good Morning")
} else if hour >= 12 && hour < 17 {
    print("Good Afternoon")
} else if hour >= 17 {
    print("Good Evening")
}

Yes you can using NSDateComponents which will return the hour in the 24 hour format.

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit fromDate:[NSDate date]];
NSInteger hour = [components hour];

if(hour >= 0 && hour < 12)
    NSLog(@"Good morning, foo");
else if(hour >= 12 && hour < 17)
    NSLog(@"Good afternoon, foo");
else if(hour >= 17)
    NSLog(@"Good evening, foo");

Swift 3

let hour = Calendar.current.component(.hour, from: Date())

if hour >= 0 && hour < 12 {
    print("Good Morning")
} else if hour >= 12 && hour < 17 {
    print("Good Afternoon")
} else if hour >= 17 {
    print("Good Evening")
}
生死何惧 2024-12-04 17:27:47

更新为 Swift 3.0

let hour = Calendar.current.component(.hour, from: Date())

if hour >= 0 && hour < 12 {
    print("Good Morning")
} else if hour >= 12 && hour < 17 {
    print("Good Afternoon")
} else if hour >= 17 {
    print("Good Evening")
}

Updated for Swift 3.0

let hour = Calendar.current.component(.hour, from: Date())

if hour >= 0 && hour < 12 {
    print("Good Morning")
} else if hour >= 12 && hour < 17 {
    print("Good Afternoon")
} else if hour >= 17 {
    print("Good Evening")
}
抽个烟儿 2024-12-04 17:27:47

使用 NSCalendar 实例从您的 NSDate 创建一个 NSDateComponents 实例,然后只需检查 的小时、分钟和秒属性NSDateComponents 并呈现适当的消息。

Use an NSCalendar instance to create a NSDateComponents instance out of your NSDate, then just check the hours, minutes and seconds property of the NSDateComponents and present the appropriate message.

叫思念不要吵 2024-12-04 17:27:47

中午12:00 PM下午是从12:01 PM5:00 PM左右。 晚上是从下午 5:01晚上 8 点,或日落前后。 夜晚是从日落到日出,即从下午8:01到上午5:59

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:[NSDate date]];
[components setTimeZone:[NSTimeZone localTimeZone]];
NSInteger hour = [components hour];

if(hour >= 6 && hour < 12)
    NSLog(@"Good morning!");
else if(hour >= 12 && hour < 17)
    NSLog(@"Good afternoon!");
else if(hour >= 17 && hour < 20)
    NSLog(@"Good evening!");
else if((hour >= 20) || (hour >= 0 && hour < 6))
    NSLog(@"Good night!");

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.

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitHour fromDate:[NSDate date]];
[components setTimeZone:[NSTimeZone localTimeZone]];
NSInteger hour = [components hour];

if(hour >= 6 && hour < 12)
    NSLog(@"Good morning!");
else if(hour >= 12 && hour < 17)
    NSLog(@"Good afternoon!");
else if(hour >= 17 && hour < 20)
    NSLog(@"Good evening!");
else if((hour >= 20) || (hour >= 0 && hour < 6))
    NSLog(@"Good night!");
会发光的星星闪亮亮i 2024-12-04 17:27:47

您可以使用类 NSDate 来获取 iPhone 上的时间。

NSDate * today = [NSDate date];
NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];

NSDateComponents * comps = [cal components:NSHourCalendarUnit fromDate:today];

if ( [comps hour]>0 && [comps hour] < 12 )
    NSLog(@"Good morning, foo");

if ( [comps hour] > 12 && [comps hour] < 17 )
    NSLog(@"Good afternoon, foo");

if ( [comps hour] >17 && [comps hour]<24  )
    NSLog(@"Good evening, foo");

参考:NSDate 文档

You could just use the class NSDate to grab the time on the iPhone.

NSDate * today = [NSDate date];
NSCalendar * cal = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];

NSDateComponents * comps = [cal components:NSHourCalendarUnit fromDate:today];

if ( [comps hour]>0 && [comps hour] < 12 )
    NSLog(@"Good morning, foo");

if ( [comps hour] > 12 && [comps hour] < 17 )
    NSLog(@"Good afternoon, foo");

if ( [comps hour] >17 && [comps hour]<24  )
    NSLog(@"Good evening, foo");

Reference: NSDate Documentation

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文