iOS当前时间需要时区吗?

标签 ios nsdate

在下面的示例中,无论用户身在何处,它会始终在用户设备上显示当前时间,还是只显示 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/

相关文章:

ios - 如何从子项目调用函数

ios - 在更改日期格式时得到错误的日期

ios - NSDateFormatter不适用于单个硬编码日期

objective-c - 为什么字符串中的 NSDate 少了一天?

ios - 将 NSDate 从一个时区更改为另一个时区

ios - 具有动态数据源页面的 UIPageViewController 被重复

ios - 在 UIButton 上显示一些动态文本

html - iOS 应用程序中 HTML 文件中的图片

iphone - 当应用程序处于前台且屏幕被锁定时播放声音 - iOS

ios - NSDateFormatter 将 rails 字符串转换为 NSDate 返回 nil?