IOS 日期转换

在数据解析中  往往服务器传回的是一个时间戳  如何转换我们常用的时间格式


-(NSString *)convertJsonDateToIOSDate:(NSString *)jsonDate
{
    //返回的Json日期应该是这样的Date(xxxxxxxxx)
    NSString *tmpStr = [jsonDate substringWithRange:NSMakeRange(6, 13)];
    //将json日期转变为ios的系统时间
    NSTimeInterval userTime = [tmpStr doubleValue];
    
    //设定时间格式,这里可以设置成自己需要的格式
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    //这里比较关键,需要将NSTimeInterval除以1000
    NSString *iosDate = [dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:userTime/1000.0]];
    
    return iosDate;
}


你可能感兴趣的:(IOS,iOS)