Introduction:
moment.js is a function library dedicated to processing time, which can be used in browsers and node.js environments.
Installation: npm i moment --save
use:
import moment from 'moment'
const moment = require('moment')
The moment introduced here is actually a function that returns a moment instance based on the parameters passed in
The moment instance actually wraps a layer of Date objects
There are a large number of methods that moment instances can use, enough to meet daily business needs.
1. Create a moment instance
1 Create an instance based on the current date: moment ();
2 Create an instance based on the date object: moment (new Date ());
3 Create an instance based on the string: moment ('2018--10--10', 'YYYY--MM--DD'); the first parameter is a time string, and the second parameter is the format of the time
If a string is a time string that conforms to the ISO 8601 standard, you do not need to pass in the second parameter, such as: moment ('2018-10-10')
For the format of time, please refer to the official website http://momentjs.cn/docs/#/parsing/
The second parameter can also be an array in time format, which is used to handle various situations where the format may exist
moment("29-06-1995", ["MM-DD-YYYY", "DD-MM", "DD-MM-YYYY"]); // uses the last format
moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]); // uses the first format
4 Create an instance based on the number of milliseconds: moment (Number)
5 Create an instance based on timestamp: moment (Number)
6 Copy a moment: moment (Moment) or var a = moment (); var b = a.clone ()
Two. According to the moment instance to obtain the format data we want
var mom = torque ();
1 Obtain the formatting string mom.format ("YYYY-MM-DD HH: mm: ss") (emphasis here, pay attention to the case of minutes and seconds) for details, refer to http://momentjs.cn/docs/#/ displaying /
2 Get and set milliseconds, seconds, day (month), day (week), week, month, and year reference documents
3 Maximum (small) date
moment.max(Moment[,Moment...]);
moment.min(Moment[,Moment...]);
Three operation moment
The moment object is a mutable object, each operation will return a new moment
1 Addition:
moment().add(7, 'days');
2 Cutting method:
moment().subtract(1,'days')
3 Chain call: moment (). Add (1, 'd'). Subtract (1, M)
4 Set the year, month, day, hour, minute, and second:
moment().year(2018).month(0).date(1).hours(0).minutes(0).seconds(0).milliseconds(0);
Four comparison
moment's time is very semantic
1. Whether before
moment().isBefore(Moment|String|Number|Date|Array);
moment().isBefore(Moment|String|Number|Date|Array, String);
The first parameter is time, which can be a moment instance or other. The second parameter is the precision of comparison (if it is year, only the year is compared, if it is day, the year, month, and day are compared)
moment('2010-10-20').isBefore('2010-10-21'); // true
2. Whether after
moment().isAfter(Moment|String|Number|Date|Array);
moment().isAfter(Moment|String|Number|Date|Array, String);
3. Are they the same
moment().isSame(Moment|String|Number|Date|Array);
moment().isSame(Moment|String|Number|Date|Array, String);
4. Whether between
moment().isBetween(moment-like, moment-like);
moment().isBetween(moment-like, moment-like, String);
// where moment-like is Moment|String|Number|Date|Array
The third parameter is also precision
5. Whether it is a leap year
moment().isLeapYear();
6. Whether it is a moment object
moment.isMoment(obj);
7 Same or before, same or after
moment().isSameOrBefore(Moment|String|Number|Date|Array);
moment().isSameOrBefore(Moment|String|Number|Date|Array, String);
moment().isSameOrAfter(Moment|String|Number|Date|Array);
moment().isSameOrAfter(Moment|String|Number|Date|Array, String);