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;
}
作者:朱祁林 出处:http://zhuqil.cnblogs.com 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
- (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 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
相关文章推荐
- objective-c计算相对于现在的时间差
- objective-c计算相对于现在的时间差
- 利用时间戳来准确计算某个时间点具现在的时间差
- js计算现在到某一个时刻还有多少时间的代码
- js距离现在时间计算
- 利用时间戳来准确计算某个时间点具现在的时间差(转)
- js 计算过去和未来的时间距离现在多少天?
- Objective-C 计算代码运行时间
- Java中时间间隔的计算(以前的时间与现在的时间间隔,类似于新浪微博显示发表时间的方式)
- iOS 计算某个时间到现在是多少月/天/时的代码
- Objective-C 计算代码运行时间
- JavaScript基础 计算现在时间 距离 过去一时间 相差多少天
- 利用时间戳来准确计算某个时间点具现在的时间差
- Objective-C 计算代码运行时间
- java DateTime计算宝宝从出生到现在的时间
- JavaScript基础 计算现在时间 距离 过去一时间 相差多少天
- Java中时间间隔的计算(以前的时间与现在的时间间隔,类似于新浪微博显示发表时间的方式)
- 计算给定时间与现在的时间差
- objective-c 中计算时间间隔
- JavaScript基础 计算现在时间 距离 未来一时间 相差多少天