iOS 处理时间带‘‘T’’的时间格式

-(NSString *)changeStringToDate:(NSString *)string {

    

    //带有T的时间格式,是前端没有处理包含时区的,强转后少了8个小时,date是又少了8个小时,所有要加16个小时。

    

    NSString *str =[string stringByReplacingOccurrencesOfString:@"T"withString:@" "];

    

    NSString *sss =[str substringToIndex:19];

    

    //    NSString *str1 =[str stringByReplacingOccurrencesOfString:@".000Z" withString:@""];

    

    NSDateFormatter *dateFromatter = [[NSDateFormatter alloc] init];

    

    [dateFromatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    

    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];

    

    [dateFromatter setTimeZone:timeZone];

    

    NSDate *date = [dateFromatter dateFromString:sss];

    

    NSDate *newdate = [[NSDate date] initWithTimeInterval:8 * 60 * 60 sinceDate:date];//

    

    NSDate *newdate1 = [[NSDate date] initWithTimeInterval:8 * 60 * 60 sinceDate:newdate];

    

    NSString *newstr =[[NSString stringWithFormat:@"%@",newdate1] substringToIndex:19];

    

    return newstr;

    

}

你可能感兴趣的:(iOS)