This is the 6th day of my participation in the wenwen Challenge
Moment. Js is a very useful date class, you can get or format dates, etc. In order not to go to the official website every time, so some of the most common use of the record.
Time to get
Gets the current time
Format ('YYYY-MM-DD HH: MM :ss') // Obtain the current timestamp moment.valueof () // Obtain the current timestamp moment(). Such as (2021-06-09 ~ 2021-06-09) moment (new Date ()), subtract (1, 'up'). The format (' YYYY - MM - DD) + '~' + moment (). The format (' - DD YYYY - MM) // Get only year, such as 2021 moment().year(); Moment ().month()+1; moment().month()+1; Moment ().date(); / / the first day of this month my moment (). The startOf (" month "). The format (' MM - DD YYYY - '); / / this year on the first day of my moment (). The startOf (" year "). The format (' MM - DD YYYY - ');Copy the code
Get the previous time
Moment ().subtract(1, 'year').format('YYYY-MM-DD'); Moment ().subtract(1, 'months').format('YYYY-MM-DD'); Weekday (-3). Format ('YYYY-MM-DD'); Moment ().subtract(5, 'days').format('YYYY-MM-DD');Copy the code
// Get the previous (year, month, day, hour, minute, second). Add (-1,'year'). Format ('YYYY-MM-DD HH: MM :ss') // Last 【 year 】 moment().add(-1,'month'). Format ('YYYY-MM-DD ') HH: mm: ss ') / / previous [on] my moment (), add (1, 'week'). The format (' mm - DD YYYY - HH: mm: ss ') / / previous "week" My moment (). The add (1, 'days'). The format (' MM - DD YYYY - HH: MM: ss') / / previous moment day 】 【 (). The add (1, 'hour'). The format (' MM - DD YYYY - HH: MM: ss ') / / a [hours] moment before (). The add (1, 'minute). The format (' MM - DD YYYY - HH: MM: ss') / / previous [minutes] my moment (). The add (1, 'second'). The format (' YYYY - MM - DD HH:mm:ss') // last [SEC]Copy the code
Gets the date after
EndOf ('month'). Format ("YYYY-MM-DD") // Result 2021-01-31Copy the code
/ / get my moment on Friday (). Weekday (5). The format (' MM - DD YYYY - '); / / on the last day of this month my moment (). The endOf (" year "). The format (' MM - DD YYYY - '); / / this year on the last day of my moment (). The endOf (" month "). The format (' MM - DD YYYY - ');Copy the code
Time formatting
Format the current time as a timestamp
/ / format timestamp (in seconds) moment (). The format (' X ') / / format timestamp (in milliseconds) moment (). The format (' X ') = = = my moment (). The valueOf ();Copy the code
Timestamp formatting
moment(1623496322380).format('YYYY-MM-DD HH:mm:ss');
Copy the code
[China Standard Time] Interchange
let time = new Date(); //Sat Jun 13 2021 14:29:52 GMT+0800 (中 式 时间) // change time to time2 = moment(time). Format (" yyyy-mm-dd HH: MM :ss") //2021-06-12 14:29:52 // Switch back to New Date(2021-06-12 14:29:52); Let time3 = moment(time).valueof () Let time4 = moment(time).hours(23).minutes(59).seconds(59).valueof () let time4 = moment(time).hours(23).seconds(59).valueof ()Copy the code
UTC format
moment().utc().format("YYYY-MM-DD HH:mm:ss")
Copy the code
A little time
Whether or not before
moment('2020-10-20').isBefore('2021-10-21'); // true
moment('2020-10-20').isBefore('2020-12-31', 'year'); // false
moment('2020-10-20').isBefore('2021-01-01', 'year'); // true
Copy the code
Is it the same (judge year month day)
When the second argument is included, it will match all units equal to or greater. Passing in month checks for month and year. Passing day checks day, month, and year.
moment('2010-10-20').isSame('2010-10-20'); // true moment('2010-10-20').isSame('2010-12-31', 'year'); // true moment('2010-01-01').isSame('2011-01-01', 'month'); / / false, different years moment (' 2010-01-01 '). IsSame (' 2010-02-01 ', 'day'); // false, different monthCopy the code
If after
The second parameter is used to determine accuracy and not just a single value to check, so using day will check the year, month, and date.
moment('2010-10-20').isAfter('2010-10-19'); // true
moment('2010-10-20').isAfter('2010-01-01', 'year'); // false
moment('2010-10-20').isAfter('2009-12-31', 'year'); // true
Copy the code
Whether between
moment('2010-10-20').isBetween('2010-10-19', '2010-10-25'); // true
Copy the code
Time to calculate
Calculate the difference diff between the two times
Moment (endTime). Diff (moment(startTime), 'years') moment(endTime). Diff (moment(startTime), 'months') moment(endTime). Take "day" as the unit; Moment (endTime). Diff (moment(startTime), 'days') moment(endTime).diff(moment(startTime),'minutes' ) moment(endTime).diff(moment(startTime), 'seconds')Copy the code
Xiao Ke love to finish click a “like” and then go! 🤞 🤞 🤞