将response头信息中的时间转换为NSDate

从头信息中拿到的Date通常是这样的:

Date = "Sun, 12 Apr 2015 03:16:27 GMT";

需求是将时间字符串转为时间戳

- (void)toDate:(NSHTTPURLResponse *)response
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setTimeStyle:NSDateFormatterFullStyle];
    [formatter setDateFormat:@"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"];
    [formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US"]];
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];
    [formatter setCalendar:[NSCalendar calendarWithIdentifier:NSGregorianCalendar]];
    
    NSString *string = response.allHeaderFields[@"Date"];
    NSDate *date = [formatter dateFromString:string];
    NSInteger time = [date timeIntervalSince1970];
    
    NSLog(@"%ld",(long)time);
}

你可能感兴趣的:(将response头信息中的时间转换为NSDate)