uniapp格式化时间为几分钟前几天前

本想在最近更新的数据上加一个状态,想想还是换成这种及时性的时间描述比较好,百度了一下,复制可用,留存备用。

filters: {
            //将时间转换成几天前,几个月前,几小时前等
            formatDate:function(datetime) {
                var dateTimeStamp = datetime * 1000;
                var minute = 1000 * 60; //把分,时,天,周,半个月,一个月用毫秒表示
                var hour = minute * 60; 
                var day = hour * 24;
                var week = day * 7;
                var halfamonth = day * 15;
                var month = day * 30;
                var now = new Date().getTime(); //获取当前时间毫秒
                var diffValue = now - dateTimeStamp; //时间差
                
                if (diffValue < 0) {
                    console.log("diffValue<0",datetime,dateTimeStamp,now,diffValue);
                    return '刚刚';
                }
                var minC = diffValue / minute; //计算时间差的分,时,天,周,月
                var hourC = diffValue / hour;
                var dayC = diffValue / day;
                var weekC = diffValue / week;
                var monthC = diffValue / month;
                var result = "2";
                if (monthC >= 1 && monthC <= 3) {
                    result = " " + parseInt(monthC) + "月前"
                } else if (weekC >= 1 && weekC <= 3) {
                    result = " " + parseInt(weekC) + "周前"
                } else if (dayC >= 1 && dayC <= 6) {
                    result = " " + parseInt(dayC) + "天前"
                } else if (hourC >= 1 && hourC <= 23) {
                    result = " " + parseInt(hourC) + "小时前"
                } else if (minC >= 1 && minC <= 59) {
                    result = " " + parseInt(minC) + "分钟前"
                } else if (diffValue >= 0 && diffValue <= minute) {
                    result = "刚刚"
                } else {
                    var datetime = new Date();
                    datetime.setTime(dateTimeStamp);
                    var Nyear = datetime.getFullYear(); {}
                    var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
                    var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
                    var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
                    var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
                    var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
                    result = Nyear + "-" + Nmonth + "-" + Ndate
                }
                return result;
            }
    

        },

过滤器写好之后调用:{{item.create_time | formatDate}}
这里传入create_time是时间戳(10位数,秒)形式,此处要*1000转换成毫秒。

image.png

格式化时间,仅将数据库中时间戳转换成时间格式

formatDate: function(value, spe = '/') {
                value = value * 1000 //小于10位则*1000
                let data = new Date(value);
                let year = data.getFullYear();
                let month = data.getMonth() + 1;
                let day = data.getDate();
                let h = data.getHours();
                let mm = data.getMinutes();
                let s = data.getSeconds();
                month = month >= 10 ? month : "0" + month;
                day = day >= 10 ? day : "0" + day;
                h = h >= 10 ? h : "0" + h;
                mm = mm >= 10 ? mm : "0" + mm;
                s = s >= 10 ? s : "0" + s;
                return `${year}${spe}${month}${spe}${day} ${h}:${mm}:${s}`;
            }

你可能感兴趣的:(uniapp格式化时间为几分钟前几天前)