iOS 时间戳

我们通常以1970年1月1日至今的毫秒数或秒数作为时间戳标记某个动作

1、时间转为时间戳

NSTimeZone *zone = [NSTimeZone defaultTimeZone];//默认时区
NSInteger interval = [zone secondsFromGMTForDate:[NSDate date]];//当前时区与世界标准时间(格林威尼时间)的时差(以秒为单位)
NSDate *localeDate = [[NSDate date] dateByAddingTimeInterval:interval];
NSTimeInterval timeInterval = [localeDate timeIntervalSince1970]; // 默认为double类型
long t = (long)timeInterval; // 可以忽略小数点后的数字,以秒为单位保存(粗略统计时)
NSString *sp = [NSString stringWithFormat:@"%ld", t];
sp = [NSString stringWithFormat:@"%f", timeInterval];
info.updateTime = sp;


2、时间戳转为时间

NSString *time = info.updateTime;
NSTimeInterval timeInterval = [time doubleValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSLog(@"date:%@", date);


你可能感兴趣的:(IOS)