zoukankan      html  css  js  c++  java
  • 时间算法 转换 (类似微信朋友圈时间显示)

    1 时间戳转换

    - (NSString *)updateTimeForRow:(NSString *)createTimeString {

        // 获取当前时时间戳 1466386762.345715 十位整数 6位小数

        NSTimeInterval currentTime = [[NSDate date] timeIntervalSince1970];

        // 创建时间戳(后台返回的时间 一般是13位数字)

        NSTimeInterval createTime = [createTimeString longLongValue]/1000;

        // 计算时间差

        NSTimeInterval time = currentTime - createTime;

        //如果时间小于60秒 显示 刚刚

        if (time < 60) {

            NSString *string = @"刚刚";

            return string;

        }

    //如果时间大于60秒  小于60分钟  显示多少分钟前

        NSInteger sec = time/60;

        if (sec<60) {

            return [NSString stringWithFormat:@"%ld分钟前",sec];

        }

    //如果时间 小于24小时  显示多少小时前

        // 秒转小时

        NSInteger hours = time/3600;

        if (hours<24) {

            return [NSString stringWithFormat:@"%ld小时前",hours];

        }

    //如果时间大于24小时  小于30天  显示年月日  时间格式自己定 (也可以显示多少天之前  此处注释掉了)

        //秒转天数

        NSInteger days = time/3600/24;

        if (days < 30) {

            

            NSString * timeString = createTimeString;

            NSTimeInterval interval=[timeString doubleValue] / 1000.0;

            NSDate *conTimesp = [NSDate dateWithTimeIntervalSince1970:interval];

            NSDateFormatter* format = [[NSDateFormatter alloc]init];

            [format setDateFormat:@"MM-dd HH:mm"];

            NSString* times = [format stringFromDate:conTimesp];

            return times;

    //        return [NSString stringWithFormat:@"%ld天前",days];

        }

    //如果时间大于30天  小于12个月  显示时间格式 

        //秒转月

        NSInteger months = time/3600/24/30;

        if (months < 12) {

            

            NSString * timeString = createTimeString;

            NSTimeInterval interval=[timeString doubleValue] / 1000.0;

            NSDate *conTimesp = [NSDate dateWithTimeIntervalSince1970:interval];

            NSDateFormatter* format = [[NSDateFormatter alloc]init];

            [format setDateFormat:@"MM-dd HH:mm"];

            NSString* times = [format stringFromDate:conTimesp];

            return times;

        }

    //其他的 就显示时间格式

        NSString * timeString = createTimeString;

        NSTimeInterval interval=[timeString doubleValue] / 1000.0;

        NSDate *conTimesp = [NSDate dateWithTimeIntervalSince1970:interval];

        NSDateFormatter* format = [[NSDateFormatter alloc]init];

        [format setDateFormat:@"MM-dd HH:mm"];

        NSString* times = [format stringFromDate:conTimesp];

        

        return times;

    }

  • 相关阅读:
    Excel 相关实用计算公式
    C# ThreadState属性分析
    U8 数据库服务器和应用服务器 分离后出现 登陆系统管理 远程组件初始化 失败 解决方案!
    MyEclipse 快捷键
    项目中用到的开源框架
    spring.net nhibernate 不一定是自增变量,也可能是触发器 Batch update returned unexpected row count from update; actual row count: 2; expected: 1
    解决方法: A C3P0Registry mbean is already registered.This probably means that an application using c3p0的警告信息处理
    NHibenate hbm.xml 自动 生成 数据库表的时候 长度为1
    解决方法: Failed to load the sqljdbc_auth.dll
    SQL 中判断 纯数字
  • 原文地址:https://www.cnblogs.com/Lovexiaohuzi/p/6688325.html
Copyright © 2011-2022 走看看