Search code examples
iosiphonensdate

Get night mode from EDSunriseSet


I have sunrise time and sunset time from EDSunriseSet, now I need to compare this with current time to get night mode:

NSCalendar *todayCal = [NSCalendar currentCalendar];
NSDateComponents *todayComp = [todayCal components: NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate:[NSDate date]];


BOOL nightMode;

if (todayComp.date > sunriseSet.localSunrise.date & todayComp.date < sunriseSet.localSunset.date) {
    nightMode = NO;

} else {
    nightMode = YES;
    
}


if (nightMode) {
    NSLog(@"the current is night zzzzzzzzzzz PM");
    
} else {
    NSLog(@"the current is day ........... AM");
    
}

But it's not working!!

sunrise: 5:18:4 - sunset: 18:41:50

current time: 19:54:13


Solution

  • check this code its work well

    NSCalendar *todayCal = [NSCalendar currentCalendar];
    NSDateFormatter *dateFormater = [[NSDateFormatter alloc]init];
    [dateFormater setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *startDate = [dateFormater dateFromString:@"2017-04-16 05:18:04"];
    NSDate *EndDate = [dateFormater dateFromString:@"2017-04-16 18:41:50"];
    NSDate *CurrentDate = [dateFormater dateFromString:@"2017-04-16 15:54:13"];
    
    BOOL nightMode;
    if (CurrentDate > startDate & CurrentDate < EndDate) {
         nightMode = NO;
    
    } else {
        nightMode = YES;
    
    }
    if (nightMode) {
        NSLog(@"the current is night zzzzzzzzzzz PM");
    } else {
        NSLog(@"the current is day ........... AM");
    }