This question already has answers here:
How to get the time in iOS Programming
                                
                                    (5个答案)
                                
                        
                                5年前关闭。
            
                    
获取用户当前时间(例如下午12:00)以执行操作的最佳方法是什么?如果用户的当前时间在晚上8点至凌晨5点之间,我有一个游戏想显示夜间图像。

最佳答案

NSDate *currentTime = [NSDate date];

NSDateFormatter *timeFormatter = [[NSDateFormatter alloc] init];
[timeFormatter setDateFormat:@"HH"];

NSString *hourCountString = [timeFormatter stringFromDate:currentTime];

int hourCountInt = [hourCountString intValue];

//time between 8PM - 5AM.
if(hourCountInt > 20 || hourCountInt < 5)
{
    NSLog(@"display night-time images");
}

08-27 19:01