更多信息
我想在我的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
协调世界时 (UTC) 和格林威治标准时间 (GMT) 之间没有时差。 您可以从这里获取更多信息
参考 :-
-(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];
}