时间戳是一种时间表示,定义从格林尼治时间1970年01月01日00时00分00秒起至现在的总秒数
//现在时间
NSDate *nowTime = [NSDate date];
//获取时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interVal = [zone secondsFromGMTForDate:nowTime];
NSDate *localTime = [nowTime dateByAddingTimeInterval:interVal];//本地时间
//时间戳转为时间
NSString *time = @"1400386922";
CGFloat dTime = [time floatValue];
NSDate *publishTime = [NSDate dateWithTimeIntervalSince1970:dTime];
NSLog(@"%@", publishTime);
//计算时间间隔(localTime - publishTime)
NSTimeInterval timeInterval = [localTime timeIntervalSinceDate:publishTime];
NSLog(@"%f", timeInterval);
if (timeInterval < 60) {
NSString *time = @"刚刚";
NSLog(@"刚刚!!");
}
if (timeInterval >= 60 && timeInterval < 3600) {
int a = timeInterval / 60;
NSString *time = [NSString stringWithFormat:@"%d分钟前", a];
NSLog(@"%@", time);
}
if (timeInterval >= 3600 && timeInterval < 3600 * 24) {
int a = timeInterval / 3600;
NSString *time = [NSString stringWithFormat:@"%d小时前", a];
NSLog(@"%@", time);
}
if (timeInterval >= 3600 * 24 && timeInterval < 3600 * 24 * 31) {
int a = timeInterval / (3600 * 24);
NSString *time = [NSString stringWithFormat:@"%d天前", a];
NSLog(@"%@", time);
}