在下面的示例中,它是否始终显示用户设备上的当前时间,无论他们身在何处,还是仅显示GMT,我是否需要获取并设置时区?
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *components =
[gregorian components:(NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond) fromDate:today];
NSInteger hour = [components hour];
NSInteger minute = [components minute];
NSInteger second = [components second];
NSLog(@"hour %d", hour);
NSLog(@"minute %d", minute);
NSLog(@"second %d", second);
答案 0 :(得分:1)
答案 1 :(得分:0)
如果要显示用户设备的当前时间,则应添加以下代码行。
components.timeZone = [NSTimeZone systemTimeZone];