2017-04-27 9 views
0

からナイトモードを取得します:私は今、私はナイトモードを取得するために、現在の時間とこれを比較する必要があり、<a href="https://github.com/erndev/EDSunriseSet" rel="nofollow noreferrer">EDSunriseSet</a>から日の出時間と日没時間が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 - 日没:午後6時41分50秒

現在の時刻:19時54分13秒

+0

であるかの問題あなたは –

+0

に直面していますそれは常に現在の夜が表示されているzzzzzzzzzzz PM –

+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"); 
} 
関連する問題