当前位置:首页 > vue.js > 正文内容

vue 时间戳转今天,昨天,以前时间

tigon4年前 (2021-03-30)vue.js2912
filters:{
        formatDate(date) {
            const lastDate = new Date(date);
lastDate.setHours(0);
lastDate.setMinutes(0);
lastDate.setSeconds(0);
            const mistiming = Math.round((Date.now() - lastDate.getTime()) / 1000)
            const tags = ['年', '个月', '星期', '天', '小时', '分钟', '秒']
            const times = [31536000, 2592000, 604800, 86400, 3600, 60, 1]
            for (let i = 0; i < times.length; i++) {
                const inm = Math.floor(mistiming / times[i])
                if (tags[i] === '天') {
                    switch (inm) {
                        case 0:
                            return '今天'
                            break
                        case 1:
                            return '昨天'
                            break
                        default:
                            // return inm + tags[i] + '前'
                            timestampToTime(date)
                            break;
                    }
                }
                if (inm !== 0) {
                    return timestampToTime(date)
                }

            }
            function timestampToTime(timestamp) {
                let date =new Date(timestamp),//时间戳为10位需*1000,时间戳为13位的话不需乘1000
                    Y = date.getFullYear(),
                    M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1),
                    D = date.getDate(),
                    h = date.getHours() +':',
                    m = date.getMinutes() +':',
                    s = date.getSeconds();
                    return Y +'-'+M +'-'+D; //时分秒可以根据自己的需求加上
                }
        },
    },

以下是具体的月星期

function formatDate(date) {
            const mistiming = Math.round((Date.now() - new Date(date).getTime()) / 1000)
            const tags = ['年', '个月', '星期', '天', '小时', '分钟', '秒']
            const times = [31536000, 2592000, 604800, 86400, 3600, 60, 1]
            for (let i = 0; i < times.length; i++) {
                const inm = Math.floor(mistiming / times[i])
                if (tags[i] === '天') {
                    switch (inm) {
                        case 0:
                            return '今天'
                            break
                        case 1:
                            return '昨天'
                            break
                        case 2:
                            return '前天'
                            break
                        default:
                            return inm + tags[i] + '前'
                            break;
                    }
                }
                if (inm !== 0) {
                    return inm + tags[i] + '前'
                }
            }
        }

除今年日期之前的都添加年

function timestampToTime(timestamp) {
                let date =new Date(timestamp),//时间戳为10位需*1000,时间戳为13位的话不需乘1000
                    Y = date.getFullYear(),
                    M = (date.getMonth() +1 <10 ?'0' + (date.getMonth() +1) : date.getMonth() +1),
                    D = date.getDate(),
                    h = date.getHours() +':',
                    m = date.getMinutes() +':',
                    s = date.getSeconds(),
                    newY=new Date().getFullYear();  //2021
                    // console.log(newY>=Y)
                    // console.log(newY,'今年时间======')  //2020
                    // console.log(Y,'去年时间======')  //2020
                    if(newY===Y){  //
                        return M +'-'+D; //时分秒可以根据自己的需求加上
                    }else{
                        return Y +'-'+M +'-'+D;
                    }

                }

版权声明:本文由Web学习之路发布,如需转载请注明出处。

本文链接:https://webge.net/?id=112

“vue 时间戳转今天,昨天,以前时间” 的相关文章

十个常用的自定义过滤器

//去除空格  type 1-所有空格  2-前后空格  3-前空格 4-后空格 function trim(value, trim) {     ...

watch监听的几种写法

var vm = new Vue({ data: { a: 1, b: 2, c: 3, d: 4, e: { f: { g: 5 } } }, watch: { a...

vue render

vue render

let element={ tagName:'ul',//节点标签名 props:{//dom的属性,用一个对象存储键值对 id:'list' }, children:[//该节点的子节点...

vue 滑入滑出效果动画

<transition> <span v-show="errorMessage" class="message"><span class="icon" :class="erro...