iOS - 新浪微博API发布微博时间返回值与当前时间对比,实现几天前,几小时前,几分钟前

iOS - 新浪微博API发布微博时间返回值与当前时间对比,实现几天前,几小时前,几分钟前

这个问题在度娘上搜了好半天都没有搜到,好不容易搜到复制代码到项目中一测试就出问题了,的出来值相差太远,为了解决这个问题,我是绞尽脑汁的想办法,查问题出在哪里,浪费了好多时间,终于找到问题所在了,原来是当前时间在对比过去时间时出来问题。

网上复制的代码:

+ (NSString *) compareCurrentTime:(NSString *)str
{

    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate *timeDate = [dateFormatter dateFromString:str];

    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%d分钟前",temp];
    }

    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%d小时前",temp];
    }

    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%d天前",temp];
    }

    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%d月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%d年前",temp];
    }

    return  result;
}

既然知道了问题所在,这下就好解决了!

我针对当前时间在对比过去时间的问题,去搜了一下都有哪些对比时间的代码。知道问题所在才能对症下药

错误代码:NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow] ;

这个错处在了[timeDate timeIntervalSinceNow]上,不可以这写,这样写得出时间差的值非常大,而且还是负值

所以我就用了下面的写法

正确代码:NSTimeInterval  timeInterval = [date2 timeIntervalSinceDate:timeDate];

就是把当前时间和过去时间去作对比,[date2 timeIntervalSinceDate:timeDate];这样还是比较好的,这样出偏差的几率很小

本以为这样就可以了,可是运行后才发现还有问题存在,然后我就从获取时间差下面开始找,问题是在NSTimeInterval timeInterval = [date2 timeIntervalSinceDate:timeDate]; 这行代码下面的两行代码中出错的

错误代码:

timeInterval = -timeInterval;

timeInterval = timeInterval - 8*60*60;

就是这两行代码,从这两行代码中打印输出后,发现所得出来的值是有问题的

没办法又去搜了,可是,没有得到自己想要的答案。一怒之下就把这两行代码给干掉了,运行后发现,我靠,好了

删掉这两行代码是对的,得出了正确的答案了

说了这么多的废话,我就把正确的代码贴出来,代码如下:

+ (NSString *)compareCurrentTime:(NSString *)str
{
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"EEE MMM d HH:mm:ss Z yyyy"];
    
    dateFormatter.locale=[[NSLocale alloc]initWithLocaleIdentifier:@"en_US"];
    
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    NSDate *date2 = [NSDate date];
    //得到与当前时间差
    NSTimeInterval  timeInterval = [date2 timeIntervalSinceDate:timeDate];
    
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
    
}

这就是正确的代码了,到处结束了!

希望能帮助到你!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,609评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,954评论 18 399
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 13,871评论 6 13
  • 最近再忙结课设计,整个人差点被修图跟建模逼疯,得亏小伙伴的及时解救,让我可以愉快的开始我的暑假生活啦,今天晚上就可...
    阳阳君阅读 6,020评论 23 36
  • 哎呦喂,这两天最火爆的娱乐圈新闻是王宝强宣布离婚的消息了。一时间全国人民都知道的赶脚。反正新一轮离婚互撕开始,全民...
    华枝春满5339阅读 2,976评论 1 1