2 abcdefgh xyz abcdeFGh_xyz 于 2012.11.28 10:26 提问

转换NSString日期为UTC日期

我需要NSString 中的日期转换为这种格式的:yyyy-MM-dd HH:mm:ss zzz。用了下面代码:

-(NSString *)setDateStringFormat:(NSString *)date 
 {
     NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
     [formatter setDateFormat:@"dd/MM/yyyy"];
     [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
     //[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
     NSDate *dateFormatted = [formatter dateFromString:date];

     [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
     [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
 return [formatter stringFromDate:dateFormatted];
 }

然后输入日期:27/03/2012
输出日期: 2012-03-27 00:00:00GMT

输出的是GMT日期,但是我想要的是UTC日期。请帮忙看看。谢谢

1个回答

vikeyToy
vikeyToy   2012.11.28 10:50
已采纳

给你提供个程序:

-(NSString *)getUTCFormateDate:(NSString *)localDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy"];

    NSDate *dateFormatted = [dateFormatter dateFromString:localDate];
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    [dateFormatter setTimeZone:timeZone];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:dateFormatted];
    [dateFormatter release];
    return dateString;
}
Csdn user default icon
上传中...
上传图片
插入图片