时间戳转日期偏差8小时

2017-06-26  本文已影响0人  ___吉

获取系统时间戳方法:

/**
 获取系统时间戳
 */
- (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];
}
上一篇下一篇

猜你喜欢

热点阅读