美文网首页
时间戳转换为日期的详细代码.

时间戳转换为日期的详细代码.

作者: BiniOSdeveloper | 来源:发表于2016-03-26 15:46 被阅读192次
    NSInteger dateTime = [model.updatedAt integerValue];
    dateTime =  dateTime / 1000;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSLog(@"33333%@",date);
    
    NSDate *currentDate = [NSDate date];
    
    NSTimeInterval time = [currentDate timeIntervalSinceDate:date];
    NSLog(@"%f",time);
    
    if ((time / (24 *3600)) > 1 ) {
        if((time / (24 * 3600)) < 15){
        NSString *str = [NSString stringWithFormat:@"更新 %d 天前",(int)time / (24 * 3600)];
        self.labelUpdateAt.text = str;
        }else {
            // 按某个格式将日期输出.
            NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
            [dateFormatter setDateFormat:@"YYYY/MM/dd"];
            NSString *dateString = [dateFormatter stringFromDate:date];
            NSLog(@"dateString:%@",dateString);
            self.labelUpdateAt.text = dateString;
        }
    }else if ((time / 3600) > 1) {
        NSString *str = [NSString stringWithFormat:@"更新 %d 小时前",(int)(time / 3600)];
        self.labelUpdateAt.text = str;
    }else if ((time / 60) > 1) {
        NSString *str = [NSString stringWithFormat:@"更新 %d 分钟前",(int)(time / 3600)];
        self.labelUpdateAt.text = str;
    }else {
        NSString *str = @"刚刚";
        self.labelUpdateAt.text = str;
    }

相关文章

网友评论

      本文标题:时间戳转换为日期的详细代码.

      本文链接:https://www.haomeiwen.com/subject/udmdlttx.html