我有EDSunriseSet的日出时间和日落时间,现在我需要将其与当前时间进行比较以获得夜间模式:
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");
}
但是它没有用!!
日出:5:18:4 - 日落:18:41:50
当前时间:19:54:13
答案 0 :(得分:1)
检查此代码是否正常工作
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");
}