js 时间转为几久前

前言:我们经常会用到一些轮播功能,展示时间多久前谁谁中奖等,或者是一些留言功能,展示多久前谁留言了,那就需要计算当前时间和距离中奖的时间的多久

        var time = '20190711200715';//
        var stringTime = time.substr(0,4)+ '/' + time.substr(4,2) + '/' +time.substr(6,2) + 
                        ' ' +  time.substr(8,2) + ':' + time.substr(10,2) + ':' + time.substr(12,2); // 将时间转为日期格式      
        var timestamp = Date.parse(new Date(stringTime));// 再将日志格式,转为时间戳,记得是毫秒的方式


   //时间转化为多久前
 var dateDiff = function (timestamp) {
        // 补全为13位
       var arrTimestamp = (timestamp + '').split('');
        for (var start = 0; start < 13; start++) {
            if (!arrTimestamp[start]) {
                arrTimestamp[start] = '0';
            }
        }
        timestamp = arrTimestamp.join('') * 1;
        var minute = 1000 * 60;
        var hour = minute * 60;
        var day = hour * 24;
        var halfamonth = day * 15;
        var month = day * 30;
        var now = new Date().getTime();
        var diffValue = now - timestamp;

        // 如果本地时间反而小于变量时间
        if (diffValue < 0) {
            return '不久前';
        }

        // 计算差异时间的量级
        var monthC = diffValue / month;
        var weekC = diffValue / (7 * day);
        var dayC = diffValue / day;
        var hourC = diffValue / hour;
        var minC = diffValue / minute;

        // 数值补0方法
        var zero = function (value) {
            if (value < 10) {
                return '0' + value;
            }
            return value;
        };

        // 使用
        if (monthC > 12) {
            // 超过1年,直接显示年月日
            return (function () {
                var date = new Date();
                date.setTime(timestamp);
                var Nyear = date.getFullYear();
                var Nmonth = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var Ndate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
                var Nhour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                var Nminute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                var Nsecond = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
                var date1 = Nyear + "-" + Nmonth + "-" + Ndate;
                // 过去的时间
                var time = new Date(date1);
                var year = time.getFullYear();
                var month = time.getMonth() + 1;
                var date2 = time.getDate();
                //现在的时间
                var nn = new Date();
                var y = nn.getFullYear();
                var m = nn.getMonth() + 1;
                var d = nn.getDate();
                return (y - year) + '年前';
            })();
        } else if (monthC >= 1) {
            return parseInt(monthC) + "月前";
        } else if (weekC >= 1) {
            return parseInt(weekC) + "周前";
        } else if (dayC >= 1) {
            return parseInt(dayC) + "天前";
        } else if (hourC >= 1) {
            return parseInt(hourC) + "小时前";
        } else if (minC >= 1) {
            return parseInt(minC) + "分钟前";
        }
        return '刚刚';
    };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值