我想在我的iOS应用程序中记录服务器时间,我正在使用NHNetworkTime库来获取网络时间,当我尝试打印以下代码时显示GMT,通过我选择UTC,如何打印日期时间在UTC?
NSDate* datetime = [NSDate networkDate];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; // Prevent adjustment to user's local time zone.
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS Z z "];
NSString* dateTimeInIsoFormatForZuluTimeZone = [dateFormatter stringFromDate:datetime];
NSLog(@"%@",dateTimeInIsoFormatForZuluTimeZone);
this is printing 2016-03-09 06:51:23.406 +0000 GMT
答案 0 :(得分:3)
协调世界时(UTC)与格林威治标准时间(GMT)之间没有时差。您可以从More info
获取更多信息答案 1 :(得分:-1)
参考: -
-(NSString *)getUTCFormateDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:localDate];
[dateFormatter release];
return dateString;
}
此外还有时间
-(NSString *)getTimeFromDate:(NSDate *)localDate
{
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
dateFormatter.dateFormat = @"MM/dd/yy";
NSString *dateString = [dateFormatter stringFromDate: localDate];
NSDateFormatter *timeFormatter = [[[NSDateFormatter alloc]init]autorelease];
timeFormatter.dateFormat = @"HH:mm:ss";
return [timeFormatter stringFromDate: localDate];
}