iOS NSDate 时间戳 计算时间间隔,多久之前
2016-10-10 本文已影响2998人
那片阳光已醉
时间计算
NSDate
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *timeDate = [dateFormatter dateFromString:model.created_at];//model.created_at 时间
//八小时时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate:timeDate];
NSDate *mydate = [timeDate dateByAddingTimeInterval:interval];
NSDate *nowDate = [[NSDate date]dateByAddingTimeInterval:interval];
//两个时间间隔
NSTimeInterval timeInterval = [mydate timeIntervalSinceDate:nowDate];
timeInterval = -timeInterval;
long temp = 0;
// NSString *time;
if (timeInterval<60) {
self.timeLabel.text = [NSString stringWithFormat:@"刚刚"];
}else if ((temp = timeInterval/60)<60){
self.timeLabel.text = [NSString stringWithFormat:@"%ld分钟前",temp];
}else if ((temp = timeInterval/(60*60))<24){
self.timeLabel.text = [NSString stringWithFormat:@"%ld小时前",temp];
}else if((temp = timeInterval/(24*60*60))<30){
self.timeLabel.text = [NSString stringWithFormat:@"%ld天前",temp];
}else if (((temp = timeInterval/(24*60*60*30)))<12){
self.timeLabel.text = [NSString stringWithFormat:@"%ld月前",temp];
}else {
temp = timeInterval/(24*60*60*30*12);
self.timeLabel.text = [NSString stringWithFormat:@"%ld年前",temp];
}