Current Date Format
= Moment curTime the let () the format. ( 'YYYY-the MM-DD HH: mm: SS' ) the console.log ( 'current time curTime:' + curTime) // curTime: 2019-01-30 10:21:11
Specify the date format
let a = '20190130' let aFormat = moment(a).format('YYYY-MM-DD') console.log('aFormat:' + aFormat)//aFormat:2019-01-30
Operation Date
// Distance Current time: seven days the let sevenDaysAgo = Moment () Subtract (. 7, 'Days') the format ( 'YYYY-the MM-DD'.. ) The console.log ( 'sevenDaysAgo:' + sevenDaysAgo) // sevenDaysAgo: 2019- 01-23 // distance current time: one week the let oneWeekAgo = Moment () Subtract (. 1, 'weeks') the format ( 'YYYY-the MM-DD'.. ) the console.log ( 'oneWeekAgo:' + oneWeekAgo) // oneWeekAgo : 2019-01-23 // distance current time: three months the let threeMonthsAgo = Moment () Subtract (. 3, "months") the format ( "the MM-DD-YYYY.". ) the console.log ( 'threeMonthsAgo:' + threeMonthsAgo) // threeMonthsAgo:2018-10-30 // Distance Current time: One year = Moment oneYearAgo the let () Subtract (. 1, "years") the format ( "the MM-DD-YYYY.". ) the console.log ( 'oneYearAgo:' + oneYearAgo) // oneYearAgo: 2018-01-30 subtraction, corresponding to : subtract () method of addition, corresponding to: add () method
Get years of age, according to the identity card number
= idCardNumber the let '234567199302019929' the let Birthday = idCardNumber.slice (. 6, 14 ) the console.log ( 'Date of birth:' + Birthday) // Date of birth: 19,930,201 the console.log ( 'the current date:'. + moment () format ( "YYYY-MM-DD")) // current date: 2019-01-30 the let Age = Moment (.) diff (Moment (Birthday), 'years' ) console.log ( 'age Age:' + age) // age Age: 25
Conversion between the date and time stamp
When it comes to the time axis of the date of drag, it is usually required to convert the date into a timestamp to operate // convert the date into a timestamp // Moment (). ValueOf () // new new a Date (). The getTime () / / Date.parse (new new a Date ()) the let timestamp1 is = Moment () UNIX ();. the let to timestamp2 = Moment () valueOf ();. the let timeStamp3 = new new a Date () the getTime ();. the let timestamp4 = Date.parse ( new new a date ()); // the converted date stamp moment (time stamp) .format () the let timestampToDate1 = moment (timestamp1 is) .format ( "the MM-DD-YYYY" ); the let timestampToDate2 = moment (to timestamp2) .format ( "YYYY-MM-DD" ); the let timestampToDate3= moment(timestamp3).format("YYYY-MM-DD"); let timestampToDate4 = moment(timestamp4).format("YYYY-MM-DD"); console.log('timestamp1:' + timestamp1);//timestamp1:1550657032 console.log('timestamp2:' + timestamp2);//timestamp2:1550657032279 console.log('timestamp3:' + timestamp3);//timestamp3:1550657032279 console.log('timestamp4:' + timestamp4);//timestamp4:1550657032000 console.log('timestampToDate1:' + timestampToDate1);//timestampToDate1:1970-01-19 console.log('timestampToDate2:' + timestampToDate2);//timestampToDate2:2019-02-20 console.log('timestampToDate3:' + timestampToDate3);//timestampToDate3:2019-02-20 console.log('timestampToDate4:' + timestampToDate4);//timestampToDate4:2019-02-20
It is seen from the above test cases:
- moment (). unix () obtained for second time stamp unit
- Stamp unit obtained moment (). ValueOf () equivalent to new Date (). GetTime () milliseconds
- Value obtained Date.parse () is in milliseconds, and the three default is 0, i.e., not specifically to the millisecond
- If you want a timestamp into a date parameter must be a moment of milliseconds , which is recognized as a millisecond, if not, an error will result
= Moment curTime the let () the format. ( 'YYYY-the MM-DD HH: mm: SS' ) the console.log ( 'current time curTime:' + curTime) // curTime: 2019-01-30 10:21:11
Specify the date format
let a = '20190130' let aFormat = moment(a).format('YYYY-MM-DD') console.log('aFormat:' + aFormat)//aFormat:2019-01-30
Operation Date
// Distance Current time: seven days the let sevenDaysAgo = Moment () Subtract (. 7, 'Days') the format ( 'YYYY-the MM-DD'.. ) The console.log ( 'sevenDaysAgo:' + sevenDaysAgo) // sevenDaysAgo: 2019- 01-23 // distance current time: one week the let oneWeekAgo = Moment () Subtract (. 1, 'weeks') the format ( 'YYYY-the MM-DD'.. ) the console.log ( 'oneWeekAgo:' + oneWeekAgo) // oneWeekAgo : 2019-01-23 // distance current time: three months the let threeMonthsAgo = Moment () Subtract (. 3, "months") the format ( "the MM-DD-YYYY.". ) the console.log ( 'threeMonthsAgo:' + threeMonthsAgo) // threeMonthsAgo:2018-10-30 // Distance Current time: One year = Moment oneYearAgo the let () Subtract (. 1, "years") the format ( "the MM-DD-YYYY.". ) the console.log ( 'oneYearAgo:' + oneYearAgo) // oneYearAgo: 2018-01-30 subtraction, corresponding to : subtract () method of addition, corresponding to: add () method
Get years of age, according to the identity card number
= idCardNumber the let '234567199302019929' the let Birthday = idCardNumber.slice (. 6, 14 ) the console.log ( 'Date of birth:' + Birthday) // Date of birth: 19,930,201 the console.log ( 'the current date:'. + moment () format ( "YYYY-MM-DD")) // current date: 2019-01-30 the let Age = Moment (.) diff (Moment (Birthday), 'years' ) console.log ( 'age Age:' + age) // age Age: 25
Conversion between the date and time stamp
When it comes to the time axis of the date of drag, it is usually required to convert the date into a timestamp to operate // convert the date into a timestamp // Moment (). ValueOf () // new new a Date (). The getTime () / / Date.parse (new new a Date ()) the let timestamp1 is = Moment () UNIX ();. the let to timestamp2 = Moment () valueOf ();. the let timeStamp3 = new new a Date () the getTime ();. the let timestamp4 = Date.parse ( new new a date ()); // the converted date stamp moment (time stamp) .format () the let timestampToDate1 = moment (timestamp1 is) .format ( "the MM-DD-YYYY" ); the let timestampToDate2 = moment (to timestamp2) .format ( "YYYY-MM-DD" ); the let timestampToDate3= moment(timestamp3).format("YYYY-MM-DD"); let timestampToDate4 = moment(timestamp4).format("YYYY-MM-DD"); console.log('timestamp1:' + timestamp1);//timestamp1:1550657032 console.log('timestamp2:' + timestamp2);//timestamp2:1550657032279 console.log('timestamp3:' + timestamp3);//timestamp3:1550657032279 console.log('timestamp4:' + timestamp4);//timestamp4:1550657032000 console.log('timestampToDate1:' + timestampToDate1);//timestampToDate1:1970-01-19 console.log('timestampToDate2:' + timestampToDate2);//timestampToDate2:2019-02-20 console.log('timestampToDate3:' + timestampToDate3);//timestampToDate3:2019-02-20 console.log('timestampToDate4:' + timestampToDate4);//timestampToDate4:2019-02-20
It is seen from the above test cases:
- moment (). unix () obtained for second time stamp unit
- Stamp unit obtained moment (). ValueOf () equivalent to new Date (). GetTime () milliseconds
- Value obtained Date.parse () is in milliseconds, and the three default is 0, i.e., not specifically to the millisecond
- If you want a timestamp into a date parameter must be a moment of milliseconds , which is recognized as a millisecond, if not, an error will result