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

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

复制代码
   
   
- (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 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值