时间戳转日期偏差8小时

获取系统时间戳方法:

/**
 获取系统时间戳
 */
- (long long)getTimestamp{
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval time = [zone secondsFromGMTForDate:date];
    NSDate *date1 = [date dateByAddingTimeInterval:time];
    long long timeStamp = [date1 timeIntervalSince1970];
    return timeStamp;
}

获取日期方法

/**
 获取系统日期
 */
- (NSString *)getDateAccordingTimestamp:(long long)timestamp formatStyle:(NSString *)formate{
    
    NSDate *nowDate = [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *formatter = nil;
    formatter = [[NSDateFormatter alloc] init];
    //解决相差8小时的问题
    [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
    [formatter setDateFormat:formate];
    return [formatter stringFromDate:nowDate];
}

你可能感兴趣的:(时间戳转日期偏差8小时)