vue In the project , You need to moment.js Mount to global ( Namely vue On the prototype chain of ), Use directly when accessing this.moment() ;
vue The project is not mounted to the global , Single file ( Single component ) use :
==>> import moment from "moment"; Then use it directly moment()
1. Initialization date / time
Initialization date :moment().format('YYYY-MM-DD');
Initialization date time :moment().format('YYYY-MM-DD HH:mm:ss');
2. format date / time
format date :moment(value).format('YYYY-MM-DD');
Format date time :moment(value).format('YYYY-MM-DD HH:mm:ss');
3. plus / reduce ==>> Must be used before operation this.moment( Date variable ) ; Convert the date to action to moment.js Date time formats that can be processed
addition :this.moment().add(1, 'months').format('YYYY-MM-DD'); ==>>
Add one month to the current date and output in the format of 'YYYY-MM-DD'
addition :this.moment(startDate).add(2, 'days').format('YYYY-MM-DD') ==>>
Specify the date (startDate) plus 2 Days and output format
by 'YYYY-MM-DD'
subtraction : this.moment().subtract(7, 'days'); ==>> Current time minus 7 day
addition :this.moment(startDate).subtract(2, 'days').format('YYYY-MM-DD') ==>>
Specify the date (startDate) Add and subtract 2 Days and output format
by 'YYYY-MM-DD'
4. Get the day of the week
Get the day of the week : this.moment().day() or this.moment(startDate).day() ==>>
current date / Specify the date What day is it
5. Get milliseconds
Get milliseconds :this.moment().day() or this.moment(startDate).valueOf()
==>> When getting the number of milliseconds for the specified time , There must be a date . Namely startDate Include date time
6. Acquisition time difference ( In milliseconds )
Two dates / Time difference :this.moment(endTime).diff(this.moment(startTime),'days' )
==>>
Time difference between start time and end time , with “ day ” Unit ;endTime and startTime It's all milliseconds
this.moment(endTime).diff(this.moment(startTime), 'minutes')
==>> Time difference between start time and end time , with “ minute ” Unit
==>> be careful : When calculating the time difference , We can use “years”,“days”,“hours”,“minutes” as well as
"seconds" Output in units !
7. The difference between two specific dates ( Days , It can also be a year )
8. Acquisition time , branch , second
principle : Using string split Method split time minutes seconds , Then use the moment Of hour,minute and second method ; Those with dates can be used
.valueof() method .
const fixStart = '08:00:00' const getHour =
this.moment().hour(Number(fixStart.split(':')[0])); const getMinute =
this.moment().minute(Number(fixStart.split(':')[1])); const getSecond =
this.moment().second(Number(fixStart.split(':')[2])); // Described as 0, Write it directly second(0)
const getHour_Minute_Second =
this.moment().hour(Number(fixStart.split(':')[0])).minute(Number(fixStart.split(':')[1])).second(0);
console.log('===== output ',getHour,getMinute,getSecond,getHour_Minute_Second);
All the results are moment.js Self time format . It can be used format Convert to the format you want , It can also be used diff Methods the time difference was calculated
9. Convert milliseconds to minutes and seconds
be careful : When the millisecond is converted to other units , When you reach the unit you want to transfer , by 1, No matter when exceeding , Insufficient 0;
as 4800000(80 minute ), Turn into the sky :0
Change to hours :1
Change to minutes :20
To seconds :0
const msTime = 4800000; //80 minute moment.duration(msTime).hours(); // Change to hours , The value is 1
moment.duration(msTime).minutes(); // Change to minutes , The value is 20
moment.duration(msTime).seconds(); // To seconds , The value is 0
Transfer to other units :
moment.duration(msTime, 'seconds'); // To seconds moment.duration(msTime, 'minutes');
// Converted into minutes moment.duration(msTime, 'hours'); // Change to hours moment.duration(msTime, 'days');
// Turn into the sky moment.duration(msTime, 'weeks'); // Turn to week moment.duration(msTime,
'months'); // Turn to month moment.duration(msTime, 'years'); // Change to year
Corresponding display format :
10. Determine whether a date is before two dates isBetween
grammar : this.moment().isBetween(moment-like, moment-like, String, String);
a. Do not include the start dates ( There are only two parameters ) ==>> Chinanet has only two parameters
this.moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
this.moment('2010-10-19').isBetween('2010-10-19', '2010-10-25'); // false
this.moment('2010-10-25').isBetween('2010-10-19', '2010-10-25'); // false
b. Do you want to include a start date ( Four parameters , It's mainly the fourth parameter ) ==>> The English net has only four parameters
The third parameter , Fixed to null;
Fourth parameter , character string ,( ) Indicates that it does not contain ,[ ] Indicates the inclusion . Start date of right bracket system , The right bracket controls the end date
this.moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null, '()');
//false this.moment('2016-10-30').isBetween('2016-10-30', '2016-12-30', null,
'[)'); //true this.moment('2016-10-30').isBetween('2016-01-01', '2016-10-30',
null, '()'); //false this.moment('2016-10-30').isBetween('2016-01-01',
'2016-10-30', null, '(]'); //true
this.moment('2016-10-30').isBetween('2016-10-30', '2016-10-30', null, '[]');
//true
This article is only a record of my learning process , For reference only , If you have any questions , Welcome to point out !
Technology