实现简易版的moment.js

时间:2023-03-09 01:39:53
实现简易版的moment.js

  github源码地址: www.baidu.com

  作者: 易怜白

  项目中使用了时间日期的处理方法,只使用了部分方法,为了不在引入第三方的库(moment.js),这里自己封装了项目中使用到的方法。

  要实现以下功能: 

new Moment()
// 返回当前的时间对象

new Moment().unix()
// 返回当前时间的秒数

Moment.unix(timestamp)
// 传入秒数,返回传入秒数的时间对象

new Moment().format('YYYY-MM-DD dd HH:mm:ss')
// 返回值 2017-06-22 四 19:46:14

Moment.unix(1498132062000).format('YYYY-MM-DD dd HH:mm:ss')
// 返回值 2017-06-22 四 19:46:14

 

  一、基础代码:

 class Moment {
     constructor(arg = new Date().getTime()) {
         this.date = new Date(arg)
     }
 }

  arg = new Date().getTime() :这里使用解构对arg添加默认值 

  二、实现unix方法

class Moment {
    constructor(arg = new Date().getTime()) {
        this.date = new Date(arg)
    }

    // getTime()返回的是毫秒数,需要转成秒数并取整
    unix() {
        return Math.round(this.date.getTime() / 1000)
    }
}

  unix方法:返回当前时间的秒数

  三、实现静态unix方法

 class Moment {
     constructor(arg = new Date().getTime()) {
         this.date = new Date(arg)
     }

     static unix(timestamp) {
         return new Moment(timestamp * 1000)
     }
 }

  静态unix方法:参数timestamp 毫秒数  返回一个Date对象

  

  四、实现format方法

class Moment {
    constructor(arg = new Date().getTime()) {
        this.date = new Date(arg)
    }

    format(formatStr) {
        const date = this.date
        const year = date.getFullYear()
        const month = date.getMonth() + 1
        const day = date.getDate()
        const week = date.getDay()
        const hour = date.getHours()
        const minute = date.getMinutes()
        const second = date.getSeconds()

        return formatStr.replace(/Y{2,4}|M{1,2}|D{1,2}|d{1,4}|H{1,2}|m{1,2}|s{1,2}/g, (match) => {
            switch (match) {
            case 'YY':
                return String(year).slice(-2)
            case 'YYY':
            case 'YYYY':
                return String(year)
            case 'M':
                return String(month)
            case 'MM':
                return String(month).padStart(2, '0')
            case 'D':
                return String(day)
            case 'DD':
                return String(day).padStart(2, '0')
            case 'd':
                return String(week)
            case 'dd':
                return weeks[week]
            case 'ddd':
                return '周' + weeks[week]
            case 'dddd':
                return '星期' + weeks[week]
            case 'H':
                return String(hour)
            case 'HH':
                return String(hour).padStart(2, '0')
            case 'm':
                return String(minute)
            case 'mm':
                return String(minute).padStart(2, '0')
            case 's':
                return String(second)
            case 'ss':
                return String(second).padStart(2, '0')
            default:
                return match
            }
        })
    }
}