Home > Article > Web Front-end > Detailed explanation of examples of implementing moment.js
The time and date processing method is used in the project, and only some methods are used. In order not to introduce third-party libraries (moment.js), the methods used in the project are encapsulated here.
To achieve the following functions:
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. Basic code:
1 class Moment { 2 constructor(arg = new Date().getTime()) { 3 this.date = new Date(arg) 4 } 5 }
arg = new Date().getTime(): Use destructuring here to add a default value to arg
2. Implement the unix method
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 }
unixMethod: Return the number of seconds of the current time
The getTime() method returns the number of milliseconds that needs to be divided by 1000 and rounded up
3. Implement static unix method
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 }
Static unixMethod: Parameter timestamp milliseconds Return a Date object
new Date( ) Parameters that can only accept milliseconds need to be multiplied by 1000
IV. Implement format method
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 method: Pass Enter 'YYY-MM-DD HH: mm:ss', 'YYY-MM-DD', etc., according to the format you want to obtain (for details, please refer to the format method of moment.js)
The padStart() method adds 0
before the month or date. At this point we have implemented three methods: unix, static unix, and format. You can check github to complete the code. If you need to implement other You can leave me a message about the date and time processing method, and I will slowly improve and add it later.
The above is the detailed content of Detailed explanation of examples of implementing moment.js. For more information, please follow other related articles on the PHP Chinese website!