sha*_*Hwk 6 cocoa-touch objective-c ios
我需要显示当前的日期和时间.
我用过 ;
NSDate *currentDateNTime = [NSDate date];
Run Code Online (Sandbox Code Playgroud)
我想拥有当前的日期和时间(应该显示系统时间而不是GMT时间).输出应采用NSDate格式而不是NSString.
例如;
NSDate *currentDateNTime = [NSDate date];
// Do the processing....
NSDate *nowDateAndTime = .....; // Output should be a NSDate and not a NSString
Run Code Online (Sandbox Code Playgroud)
由于所有的NSDate都是GMT引用的,你可能想要这样:(不要忘记nowDate不是实际的当前系统日期时间,而是"移位",所以如果你使用NSDateFormatter生成NSString,你会看到错误的日期)
NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* nowTimeZone = [NSTimeZone systemTimeZone];
NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger nowGMTOffset = [nowTimeZone secondsFromGMTForDate:currentDate];
NSTimeInterval interval = nowGMTOffset - currentGMTOffset;
NSDate* nowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];
Run Code Online (Sandbox Code Playgroud)
每个时刻都是世界各地的同一时刻 - 它只是表示为不同时区的不同时钟时间.因此,您无法将日期更改为表示时区中某个时间的其他日期; 你必须使用NSDateFormatter你所在时区所提供的数据.结果字符串是你职位的时钟时间表示的时刻.
在GMT中完成所有需要的计算,只需使用格式化程序进行显示.
| 归档时间: |
|
| 查看次数: |
19418 次 |
| 最近记录: |