abcdeFGh_xyz
2012-11-28 02:26
采纳率: 69.2%
浏览 3.8k
已采纳

转换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 2012-11-28 02: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;
    }
    
    点赞 评论

相关推荐 更多相似问题