iOS将UTC字符串转换为本地日期

时间:2015-10-22 13:11:12

标签: ios xcode

代码:

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSDate *utc = [fmt dateFromString:strTime];

    NSTimeZone *tz = [NSTimeZone defaultTimeZone];
    NSInteger seconds = [tz secondsFromGMTForDate: utc];

    NSDate *localdate = [NSDate dateWithTimeInterval:seconds sinceDate:utc];
    NSString *local = [fmt stringFromDate:localdate];
    NSLog(@"local time%@", local);

  NSDate *localDate = [fmt dateFromString:local];

我从服务器获取UTC字符串。我成功转换为本地字符串然后我尝试将其转换为本地日期。但是日期以UTC格式显示。我的代码出了什么问题?任何帮助都会受到赞赏。谢谢提前

1 个答案:

答案 0 :(得分:0)

试试这个:

NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *utc = [fmt dateFromString:<Your_UTC_String>];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSLog(@"%@", local);
相关问题