首頁  >  文章  >  web前端  >  實作moment.js的實例詳解

實作moment.js的實例詳解

零下一度
零下一度原創
2017-06-23 10:42:282354瀏覽

專案中使用了時間日期的處理方法,只使用了部分方法,為了不在引入第三方的程式庫(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:14Moment.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 }

  一、基礎程式碼:

 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 }

  

arg = new Date().getTime()

 :這裡使用解構對arg新增預設值

 

  二、實作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

方法:傳回目前時間的秒數

  getTime()方法傳回的是毫秒數需要除1000取整處理

 

  三、實作靜態unix方法

 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 }

#  

靜態unix

方法:參數timestamp 毫秒數 回傳一個Date物件

  new Date( )只能接受毫秒的參數需要乘1000

  

#  四、實作format方法rrreee

#  

format

#rrreee

#  ###format###方法:傳入'YYY-MM-DD HH:mm:ss','YYY-MM-DD' 等,依照自己的傳入要取得的格式(具體可參考moment.js的format的方法)####### padStart()方法為月份或日期前補0######  ######  到這裡我們就實現了unix,靜態unix,format三個方法,完成程式碼可以去查看github,如果需要實現其它的日期時間處理方法可以給我留言,後面我會慢慢完善添加。 ###

以上是實作moment.js的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn