Tom Fobear
Tom Fobear

Reputation: 6749

Find if current time is between a range

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"

Upvotes: 11

Views: 5600

Answers (5)

Antony Raphel
Antony Raphel

Reputation: 2078

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!");

Upvotes: 0

Joe
Joe

Reputation: 57169

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")
}

Upvotes: 53

Chris Byatt
Chris Byatt

Reputation: 3819

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")
}

Upvotes: 2

Legolas
Legolas

Reputation: 12325

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

Upvotes: -1

JustSid
JustSid

Reputation: 25318

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.

Upvotes: 1

Related Questions