Table of contents
Installation, introduction and use
Basic use of Moment.js_npm moment adds year_CSH_lucky's blog-CSDN blog
A JavaScript date library for parsing, validating, manipulating and formatting dates
Installation, introduction and use
Install
npm install moment
introduce
//require导入 var moment = require('moment'); //import导入 import moment from 'moment';
Simple to use
引入 import moment from 'moment' 使用 任意时间戳格式化,以YYYY-MM-DD HH:mm:ss形式显示 for (let i = 0; i < res.data.content.length; i++) { res.data.content[i].replyTime = moment(new Date(res.data.content[i].replyTime)).format('YYYY-MM-DD HH:mm:ss') }
Get Time
1. Get the current time
moment()
2. Get the current year
moment().year()
moment().get('year')
3. Get the current month
moment().month() // (0~11)
moment ().get('month') //The month here starts from 0, and the current month is +1
4. Get the current date
moment().date();
moment().get('date')
5. Get the hour
moment().hours()
moment().get('hours')
6. Get minutes
moment().minutes()
moment().get('minutes')
7. Get seconds
moment().seconds()
moment().get('seconds')
Set time
1.Set the current year
moment().year()
moment().get('year')
2. Set the current monthmoment().month() // (0~11) moment().get('month') //此处月份从0开始,当前月要+1
3.Set the current datemoment().date(15) moment().set('date', 15)
4. Set hoursmoment().year(2019) moment().set('year', 2019)
5. Set minutesmoment().minutes(30) moment().set('minutes', 30)
6. Set secondsmoment().seconds(30) moment().set('seconds', 30)
Set the obtained time format
1. Get the time of the day and display it in the form of YYYY year MM month DD day
//2017-12-14 var now=moment().format("YYYY年MM月DD日");
2. Format any timestamp and display it in the form of YYYY-MM-DD HH:mm:ss
//2017-12-14T16:34:10 var t1=moment(1411641720000).format('YYYY-MM-DD HH:mm:ss');
3. Get tomorrow’s date in the format YYYY-MM-DD.
var t2=moment().day(1).format('YYYY-MM-DD');
4. Get the time dynamically, in the format YYYY-MM-DDTHH:mm
//2017-12-14T16:34 var t3 = moment(value).format('YYYY-MM-DDTHH:mm')