如何在iPhone中将NSDate转换为UTC格式

时间:2013-05-01 13:44:01

标签: iphone time sdk nsdate

我需要将当前日期转换为UTC格式,我已完成以下操作并获得了正确的UTC日期,下面是我的源代码

NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSLog(@"%@",dateString);

我正在以这种格式获取日期2013-05-01 23:11:02 GMT只有我想在GMT上使用UTC名称我需要为此做什么转换。

1 个答案:

答案 0 :(得分:1)

如果您只关心文本而不是替换字符串:

 NSString *convString = [dateString stringByReplacingOccurrencesOfString:@"GMT" withString:@"UTC"];