timeFormatter

github地址: https://github.com/neobug/timeFormatter
时间格式化 根据上传的时间戳对应的字符串,
跟当前时间做对比, 24小时内显示 : xx小时前/xx分钟前/刚刚.
24小时-48小时显示:昨天.
48小时-72小时显示:前天
超过72小时显示对应日期 和 时间: 2016-04-05 13:23

  • (void)viewDidLoad {
    [super viewDidLoad];
//    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//    //设定时间格式,这里可以设置成自己需要的格式
//    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSString *currentDateStr = [dateFormatter stringFromDate:[NSDate date]];
//    //输出格式为:2010-10-27 10:22:13
//    NSLog(@"%@",currentDateStr);


NSDate *datenow = [NSDate date];
NSString *s2 = [NSString stringWithFormat:@"%.f",[datenow timeIntervalSince1970] - 60*60*48];
NSLog(@"~~~~%@",s2);

NSString *s =  [self timeContrast:s2];//@"1459406857"];
NSLog(@"s = %@",s);

}

//输入时间戳 对应的字符串 如:1459406857; 返回对应格式的时间(跟当前时间做对比)

  • (NSString *)timeContrast:(NSString *)update_at {
//获取当前时间
NSDate *dateNow = [NSDate date];
NSString *timeStampNow = [NSString stringWithFormat:@"%.f",[dateNow timeIntervalSince1970]];


float time = (float)([timeStampNow intValue] - [update_at longLongValue]/1000);


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//设定时间格式,这里可以设置成自己需要的格式



NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:[update_at doubleValue]/1000];

[dateFormatter setDateFormat:@"MM"];
NSString * nowDay1 = [dateFormatter stringFromDate:[NSDate date]];
NSString * lastDay1 = [dateFormatter stringFromDate:beDate];



[dateFormatter setDateFormat:@"dd"];
NSString * nowDay = [dateFormatter stringFromDate:[NSDate date]];
NSString * lastDay = [dateFormatter stringFromDate:beDate];

[dateFormatter setDateFormat:@"HH:mm"];


NSString *s = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[update_at longLongValue]/1000]]];

if (time/3600 <24.0) {
    if (time/60 < 1.0){
        s = [NSString stringWithFormat:@"刚刚"];
        
    }else if (time/60<60 && time/60 >= 1.0) {
        float minute = time/60;
        s = [NSString stringWithFormat:@"%.f分钟前",minute];
    }else if (time/60 >=60 && [nowDay intValue] == [lastDay intValue]) {
        
        s = [NSString stringWithFormat:@"今天 %@",s];
        
        
    }
    
    
}else if ( ([nowDay intValue] - [lastDay intValue] == 1) && [nowDay1 intValue] == [lastDay1 intValue]) {

    s = [NSString stringWithFormat:@"昨天 %@",s];
    
    
}else if (([nowDay intValue] - [lastDay intValue] == 2) && [nowDay1 intValue] == [lastDay1 intValue]) {

    s = [NSString stringWithFormat:@"前天 %@",s];
    
    
}else {
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    s = [NSString stringWithFormat:@"%@",[dateFormatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:[update_at longLongValue]/1000]]];
    
}
return s;

}

注意:返回的 update_at时间戳 是否是多少位,如果包含后面3个0,需要用 [update_at longlongValue]
且 要 [update_at longlongValue] / 1000 .

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

推荐阅读更多精彩内容

  • 1.badgeVaule气泡提示 2.git终端命令方法> pwd查看全部 >cd>ls >之后桌面找到文件夹内容...
    i得深刻方得S阅读 4,805评论 1 9
  • 简介 用简单的话来定义tcpdump,就是:dump the traffic on a network,根据使用者...
    保川阅读 5,992评论 1 13
  • 漆黑的夜晚、狭窄的巷子,滴滴答答的雨点落在青色的石板上,橘黄色的灯光忽明忽暗。我攥着你的衣角低着头慢慢地向前移动着...
    拾土阅读 423评论 8 11
  • 《纸牌屋》火了,如果你是记者或者公关(GR、PR),还没看过,我只能说你OUT了,因为这个剧应该是记者和公关从业者...
    全神贯注阅读 485评论 0 1
  • 上月参加王永存老师微课,学到了好几个在改善和精益思维框架下的思考,借鉴。 1. 比较欧美和丰田/日式的差别。自己的...
    Thinkpolo阅读 336评论 1 1