实现简易版的moment.js

  代码github地址:https://github.com/haozhaohang/library

  作者: 易怜白

 

  项目中使用了时间日期的处理方法,只使用了部分方法,为了不在引入第三方的库(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方法

 1 class Moment {
 2     constructor(arg = new Date().getTime()) {
 3         this.date = new Date(arg)
 4     }
 5 
 6     // getTime()返回的是毫秒数,需要转成秒数并取整
 7     unix() {
 8         return Math.round(this.date.getTime() / 1000) 9  } 10 }

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

  getTime()方法返回的是毫秒数需要除1000取整处理

 

  三、实现静态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对象

  new Date()只能接受毫秒的参数需要乘1000

  

  四、实现format方法

 1 class Moment {
 2     constructor(arg = new Date().getTime()) {
 3         this.date = new Date(arg)
 4     }
 5 
 6     format(formatStr) {
 7         const date = this.date 8 const year = date.getFullYear() 9 const month = date.getMonth() + 1 10 const day = date.getDate() 11 const week = date.getDay() 12 const hour = date.getHours() 13 const minute = date.getMinutes() 14 const second = date.getSeconds() 15 16 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) => { 17 switch (match) { 18 case 'YY': 19 return String(year).slice(-2) 20 case 'YYY': 21 case 'YYYY': 22 return String(year) 23 case 'M': 24 return String(month) 25 case 'MM': 26 return String(month).padStart(2, '0') 27 case 'D': 28 return String(day) 29 case 'DD': 30 return String(day).padStart(2, '0') 31 case 'd': 32 return String(week) 33 case 'dd': 34 return weeks[week] 35 case 'ddd': 36 return '周' + weeks[week] 37 case 'dddd': 38 return '星期' + weeks[week] 39 case 'H': 40 return String(hour) 41 case 'HH': 42 return String(hour).padStart(2, '0') 43 case 'm': 44 return String(minute) 45 case 'mm': 46 return String(minute).padStart(2, '0') 47 case 's': 48 return String(second) 49 case 'ss': 50 return String(second).padStart(2, '0') 51 default: 52 return match 53  } 54  }) 55  } 56 }

  format方法: 传入'YYY-MM-DD HH:mm:ss','YYY-MM-DD' 等,根据自己的传入要获取的格式(具体可参看moment.js的format的方法)

  padStart()方法为月份或日期前补0

  

  到这里我们就实现了unix,静态unix,format三个方法,完成代码可以去查看github,如果需要实现其它的日期时间处理方法可以给我留言,后面我会慢慢完善添加。

 

转载于:https://www.cnblogs.com/hzh-fe/p/7067106.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值