js时间过滤器(x分钟前/x天前/x小时前)

68 阅读1分钟

 

    /**
     *
     * @param {Date} time
     * @return String
     */ 
    diffTime (time) {
        var endDate = new Date().getTime();
        var startDate = new Date(time).getTime();
        var diff = endDate - startDate;
        var days = Math.floor(diff / (24 * 3600 * 1000));
        var leave1 = diff % (24 * 3600 * 1000);
        var hours = Math.floor(leave1 / (3600 * 1000));
        var leave2 = leave1 % (3600 * 1000);
        var minutes = Math.floor(leave2 / (60 * 1000));
        var leave3 = leave2 % (60 * 1000);
        var seconds = Math.round(leave3 / 1000);
        var returnStr = seconds + '秒';
        var nian = 0; var yue = 0; var ri = 0;
        if (days > 365) {
            nian = new Date(parseInt(startDate)).getFullYear();
            yue = new Date(startDate).getMonth() + 1;
            if (yue < 10) { yue = '0' + yue; }
            ri = new Date(parseInt(startDate)).getDate();
            if (ri < 10) { ri = '0' + ri; }
            returnStr = nian + '.' + yue + '.' + ri;
        } else if (days > 0 && days < 365) {
            nian = new Date(parseInt(startDate)).getFullYear();
            yue = new Date(startDate).getMonth() + 1;
            if (yue < 10) { yue = '0' + yue; }
            ri = new Date(parseInt(startDate)).getDate();
            if (ri < 10) { ri = '0' + ri; }
            returnStr = nian + '.' + yue + '.' + ri;
        } else if (days < 1 && hours > 0) {
            returnStr = hours + '小时前';
        } else if (days < 1 && hours < 1 && minutes > 0) {
            returnStr = minutes + '分钟前';
        } else if (minutes < 1 && seconds > 0) {
            returnStr = '刚刚';
        }
        return returnStr;
    },

第二种:

    /**
        *
        * 传入Date对象 || 毫秒数值
        * 返回传入时间距离当前时间间隔
        * @param {Date} date
        * @return String
        *
    */
    timeAgo (date) {
        var seconds = Math.floor((new Date() - new Date(date)) / 1000);

        const yearseconds = 365 * 24 * 60 * 60;// 一年有多少秒
        const monthseconds = 30 * 24 * 60 * 60; //  一个月有多少秒
        const dateseconds = 24 * 60 * 60;// 一天有多少秒

        let interval = 0;

        if (seconds > yearseconds) {
            return createAt(date);
        } else if (seconds > monthseconds) {
            interval = Math.floor(seconds / monthseconds);
            return interval + '月前';
        } else if (seconds > dateseconds) {
            interval = Math.floor(seconds / dateseconds);
            return interval + '天前';
        } else if (seconds > 3600) {
            interval = Math.floor(seconds / 3600);
            return interval + '小时前';
        } else if (seconds > 60) {
            interval = Math.floor(seconds / 60);
            return interval + '分钟前';
        } else {
            return '刚刚';
        }

        function createAt (date) {
            var time = new Date(date);
            var year = time.getFullYear();
            var month = time.getMonth() + 1;
            var day = time.getDate();
            var hh = time.getHours();
            var mm = time.getMinutes();
            return year + '年' + month + '月' + day + '日' + ' ' + hh + ':' + mm;
        }
    },