在下面的示例中,无论用户身在何处,它会始终在用户设备上显示当前时间,还是只显示 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);
最佳答案
From苹果指南:
By default, NSCalendar uses the default time zone for the application—or process—when the calendar object is created. Unless the default time zone has been otherwise set, it is the time zone set in System Preferences.
因此,您不需要对代码进行任何更改。
关于iOS当前时间需要时区吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796116/