js、vue、vue3.0、时间格式转换函数

时间戳转换时间格式

// 时间戳转任意时间格式 调用方法=>  this.getDate(1626137166000, 'm-d H:i') this.getDate(1626137166000) 等等
getDate: function (date, format) {
        date = date || new Date()
        format = format || 'Y-m-d H:i:s'
        return this.format(format, this.beginOfDate(date).getTime() / 1000)
    },


  format: function (format, timestamp) {

        let that = this;
        let jsdate, f;
        let txt_words = [
            'Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur',
            'January', 'February', 'March', 'April', 'May', 'June',
            'July', 'August', 'September', 'October', 'November', 'December'
        ];

        let formatChr = /\\?(.?)/gi;
        let formatChrCb = function (t, s) {
            return f[t] ? f[t]() : s
        };
        let _pad = function (n, c) {
            n = String(n);
            while (n.length < c) {
                n = '0' + n
            }
            return n
        };
        f = {
       
            d: function () {
            
                return _pad(f.j(), 2)
            },
            D: function () {
        
                return f.l()
                    .slice(0, 3)
            },
            j: function () {
        
                return jsdate.getDate()
            },
            l: function () {
             
                return txt_words[f.w()] + 'day'
            },
            N: function () {
                return f.w() || 7
            },
            S: function () {
                let j = f.j();
                let i = j % 10;
                if (i <= 3 && parseInt((j % 100) / 10, 10) == 1) {
                    i = 0
                }
                return ['st', 'nd', 'rd'][i - 1] || 'th'
            },
            w: function () {
                return jsdate.getDay()
            },
            z: function () {
                let a = new Date(f.Y(), f.n() - 1, f.j());
                let b = new Date(f.Y(), 0, 1);
                return Math.round((a - b) / 864e5)
            },
            W: function () {
                let a = new Date(f.Y(), f.n() - 1, f.j() - f.N() + 3);
                let b = new Date(a.getFullYear(), 0, 4);
                return _pad(1 + Math.round((a - b) / 864e5 / 7), 2)
            },

            F: function () {
                return txt_words[6 + f.n()];
            },
            m: function () {
                return _pad(f.n(), 2)
            },
            M: function () {
                return f.F()
                    .slice(0, 3)
            },
            n: function () {
                return jsdate.getMonth() + 1
            },
            t: function () {
                return (new Date(f.Y(), f.n(), 0))
                    .getDate()
            },

            // 年
            L: function () {
                let j = f.Y()
                return j % 4 === 0 & j % 100 !== 0 | j % 400 === 0
            },
            o: function () {
                let n = f.n()
                let W = f.W()
                let Y = f.Y()
                return Y + (n === 12 && W < 9 ? 1 : n === 1 && W > 9 ? -1 : 0)
            },
            Y: function () {
                return jsdate.getFullYear()
            },
            y: function () {
                return f.Y()
                    .toString()
                    .slice(-2)
            },
            a: function () {
                return jsdate.getHours() > 11 ? 'pm' : 'am'
            },
            A: function () {
                return f.a()
                    .toUpperCase()
            },
            B: function () {
                let H = jsdate.getUTCHours() * 36e2
                let i = jsdate.getUTCMinutes() * 60
                let s = jsdate.getUTCSeconds()
                return _pad(Math.floor((H + i + s + 36e2) / 86.4) % 1e3, 3)
            },
            g: function () {
                return f.G() % 12 || 12
            },
            G: function () {
                return jsdate.getHours()
            },
            h: function () {
                return _pad(f.g(), 2)
            },
            H: function () {
                return _pad(f.G(), 2)
            },
            i: function () {
                return _pad(jsdate.getMinutes(), 2)
            },
            s: function () {
                return _pad(jsdate.getSeconds(), 2)
            },
            u: function () {
                return _pad(jsdate.getMilliseconds() * 1000, 6)
            },

           
            e: function () {
               
                throw 'Not supported (see source code of date() for timezone on how to add support)'
            },
            I: function () {
              
                let a = new Date(f.Y(), 0)
              
                let c = Date.UTC(f.Y(), 0)
               
                let b = new Date(f.Y(), 6)
              
                let d = Date.UTC(f.Y(), 6)
                return ((a - c) !== (b - d)) ? 1 : 0
            },
            O: function () {
               
                let tzo = jsdate.getTimezoneOffset()
                let a = Math.abs(tzo)
                return (tzo > 0 ? '-' : '+') + _pad(Math.floor(a / 60) * 100 + a % 60, 4)
            },
            P: function () {
               
                let O = f.O()
                return (O.substr(0, 3) + ':' + O.substr(3, 2))
            },
            T: function () {
               
                return 'UTC'
            },
            Z: function () {
                
                return -jsdate.getTimezoneOffset() * 60
            },

           
            c: function () {
               
                return 'Y-m-d\\TH:i:sP'.replace(formatChr, formatChrCb)
            },
            r: function () {
              
                return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb)
            },
            U: function () {
               
                return jsdate / 1000 | 0;
            }
        };
        this.date = function (format, timestamp) {
            that = this;
            jsdate = (timestamp === undefined ? new Date() : 
                    (timestamp instanceof Date) ? new Date(timestamp) : 
                        new Date(timestamp * 1000) 
            );
            return format.replace(formatChr, formatChrCb)
        };
        return this.date(format, timestamp)
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值