Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if time is day/night Objective C [duplicate]

I would like some opinions/help with creating a fairly simple function, here are the requirements:

I need to determine if the current time is day or night (Day is between 6am and 6pm). For example here is some pseudo code:

if(currentTime < 6pm && currentTime > 6am){
     return timeIsDay
}
else{
     return timeIsNight
}

This is for an IOS application in Objective C and MUST be IOS 8.0 compatible.

like image 918
SJTriggs Avatar asked Dec 20 '22 11:12

SJTriggs


1 Answers

Try this

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH.mm"];
    NSString *strCurrentTime = [dateFormatter stringFromDate:[NSDate date]];

NSLog(@"Check float value: %.2f",[strCurrentTime floatValue]);
    if ([strCurrentTime floatValue] >= 18.00 || [strCurrentTime floatValue]  <= 6.00){
        NSLog(@"It's night time");
    }else{
        NSLog(@"It's day time");
    }
like image 83
Deepak Avatar answered Dec 22 '22 01:12

Deepak