您的位置:首页 > 移动开发 > Objective-C

objective-c计算相对于现在的时间差

2017-11-04 20:19 190 查看
  最近做了一个小应用程序,是读取新浪微博的。微博上面对于新发的微博,不是告诉你具体什么时候发布的,而是告诉你几秒钟之前,几分钟之前,几个小时之前之类的相对于现在的时间。可以使用下面代码来计算这个时间差。

- (NSString*)timestamp
{
// Calculate distance time string
//
time_t now;
time(&now);

int distance = (int)difftime(now, createdAt);
if (distance < 0) distance = 0;

if (distance < 60) {
self.timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "second ago" : "seconds ago"];
}
else if (distance < 60 * 60) {
distance = distance / 60;
self.timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "minute ago" : "minutes ago"];
}
else if (distance < 60 * 60 * 24) {
distance = distance / 60 / 60;
self.timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "hour ago" : "hours ago"];
}
else if (distance < 60 * 60 * 24 * 7) {
distance = distance / 60 / 60 / 24;
self.timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "day ago" : "days ago"];
}
else if (distance < 60 * 60 * 24 * 7 * 4) {
distance = distance / 60 / 60 / 24 / 7;
self.timestamp = [NSString stringWithFormat:@"%d %s", distance, (distance == 1) ? "week ago" : "weeks ago"];
}
else {
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
}

NSDate *date = [NSDate dateWithTimeIntervalSince1970:createdAt];
self.timestamp = [dateFormatter stringFromDate:date];
}
return timestamp;
}

//得到当前的日期
NSDate *date = [NSDate date];
NSLog(@"youjieq date:%@",date);
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *timeStr = [formatter stringFromDate:date];
NSLog(@"today %@", timeStr);   //2017-11-04

NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [[NSDateComponents alloc] init];
NSInteger unitFlags =   NSCalendarUnitYear |
NSCalendarUnitMonth |
NSCalendarUnitDay |
NSCalendarUnitWeekday |
NSCalendarUnitHour |
NSCalendarUnitMinute |
NSCalendarUnitSecond;
comps = [calendar components:unitFlags fromDate:date];
NSInteger week = [comps weekday];
NSInteger year=[comps year];
NSInteger month = [comps month];
NSInteger day = [comps day];
//[formatter setDateStyle:NSDateFormatterMediumStyle];
//This sets the label with the updated time.
NSInteger hour = [comps hour];
NSInteger min = [comps minute];
NSInteger sec = [comps second];
NSLog(@"week%zi",week);
NSLog(@"year%zi",year);
NSLog(@"month%zi",month);
NSLog(@"day%zi",day);
NSLog(@"hour%zi",hour);
NSLog(@"min%zi",min);
NSLog(@"sec%zi",sec);


// 时间1
NSDate *date1 = [NSDate date];
NSTimeZone *zone1 = [NSTimeZone systemTimeZone];
NSInteger interval1 = [zone1 secondsFromGMTForDate:date1];
NSDate *localDate1 = [date1 dateByAddingTimeInterval:interval1];

// 时间2
NSDate *date2 = [NSDate date];
NSTimeZone *zone2 = [NSTimeZone systemTimeZone];
NSInteger interval2 = [zone2 secondsFromGMTForDate:date2];
NSDate *localDate2 = [date2 dateByAddingTimeInterval:interval2];

// 时间2与时间1之间的时间差(秒)
double intervalTime = [localDate2 timeIntervalSinceReferenceDate] - [localDate1 timeIntervalSinceReferenceDate];

NSInteger seconds = lTime % 60;
NSInteger minutes = (lTime / 60) % 60;
NSInteger hours = (lTime / 3600);
NSInteger days = lTime/60/60/24;
NSInteger month = lTime/60/60/24/12;
NSInteger years = lTime/60/60/24/365;


作者:朱祁林 出处:http://zhuqil.cnblogs.com 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: