码迷,mamicode.com
首页 > Web开发 > 详细

实现简易版的moment.js

时间:2017-06-22 20:12:32      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:seconds   处理   pre   返回   date   默认   tor   his   静态   

  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

 

  一、基础代码:

1 class Moment {
2     constructor(arg = new Date().getTime()) {
3         this.date = new Date(arg)
4     }
5 }

  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方法

1 class Moment {
2     constructor(arg = new Date().getTime()) {
3         this.date = new Date(arg)
4     }
5 
6     static unix(timestamp) {
7         return new Moment(timestamp * 1000)
8     }
9 }

  静态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
            }
        })
    }
}

实现简易版的moment.js

标签:seconds   处理   pre   返回   date   默认   tor   his   静态   

原文地址:http://www.cnblogs.com/hzh-fe/p/7066878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!