Summary of Common Usages of Moment.js
Moment.js is a lightweight JavaScript time library, which facilitates the operation of time in daily development and improves the development efficiency.
In daily development, the following operations are usually performed on time: acquiring time, setting time, formatting time, comparing time and so on. Next, I will sort out Doc s in Moment.js according to these operations, so as to facilitate learning and future reference.
Acquisition time
-
Start of Time
moment().startOf(String)
-
Get today's 0:00, 0 seconds
moment().startOf('day')
-
Get 0:0, 0 seconds on the first day of the week (Sunday)
moment().startOf('week')
-
Get 10:00 p.m. and 0 seconds on Monday this week
moment().startOf('isoWeek')
-
Get 0:0, 0 seconds on the first day of the current month
moment().startOf('month')
-
-
End of Time
moment().endOf(String)
-
Get today at 23:59:59:59
moment().endOf('day')
-
Get 23:59, 59 seconds on the last day of the week (Saturday)
moment().endOf('week')
-
Get 23:59:59 seconds on Sunday this week
moment().endOf('isoWeek')
-
Get 23:59:59 seconds on the last day of the current month
moment().endOf('month')
-
-
Days in Month
moment().daysInMonth()
-
Get the total number of days in the current month
moment().daysInMonth()
-
-
Timestamp
-
Get the timestamp (in seconds)
moment().format('X') // Return value is string type moment().unix() // Return value is numeric
-
Get the timestamp (in milliseconds)
moment().format('x') // Return value is string type moment().valueOf() // Return value is numeric
-
-
Get Time
-
Year of acquisition
moment().year() moment().get('year')
-
Get month
moment().month() (0~11, 0: January, 11: December) moment().get('month')
-
Get one day in a month
moment().date() moment().get('date')
-
Get a day of the week
moment().day() (0~6, 0: Sunday, 6: Saturday) moment().weekday() (0~6, 0: Sunday, 6: Saturday) moment().isoWeekday() (1~7, 1: Monday, 7: Sunday) moment().get('day') mment().get('weekday') moment().get('isoWeekday')
-
Acquisition hours
moment().hours() moment().get('hours')
-
Get minutes
moment().minutes() moment().get('minutes')
-
Get seconds
moment().seconds() moment().get('seconds')
-
Get the current year, month, day, time, second
moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds] moment().toObject() // {years: xxxx, months: x, date: xx ...}
-
Setup time
-
Set Time
moment().year(Number), moment().month(Number)... moment().set(String, Int) moment().set(Object)
-
Set Year
moment().year(2019) moment().set('year', 2019) moment().set({year: 2019})
-
Set month
moment().month(11) (0~11, 0: January, 11: December) moment().set('month', 11)
-
Setting up a day in a month
moment().date(15) moment().set('date', 15)
-
Setting up a day of the week
moment().weekday(0) // Set the date for the first day of the week (Sunday) moment().isoWeekday(1) // Set the date for Monday this week moment().set('weekday', 0) moment().set('isoWeekday', 1)
-
Set hours
moment().hours(12) moment().set('hours', 12)
-
Set minute
moment().minutes(30) moment().set('minutes', 30)
-
Set seconds
moment().seconds(30) moment().set('seconds', 30)
-
-
Add Time
moment().add(Number, String) moment().add(Object)
-
Set Year
moment().add(1, 'years') moment().add({years: 1})
-
Set month
moment().add(1, 'months')
-
Setting date
moment().add(1, 'days')
-
Week set up
moment().add(1, 'weeks')
-
Set hours
moment().add(1, 'hours')
-
Set minute
moment().add(1, 'minutes')
-
Set seconds
moment().add(1, 'seconds')
-
-
Subtract Time
moment().subtract(Number, String) moment().subtract(Object)
-
Set Year
moment().subtract(1, 'years') moment().subtract({years: 1})
-
Set month
moment().subtract(1, 'months')
-
Setting date
moment().subtract(1, 'days')
-
Week set up
moment().subtract(1, 'weeks')
-
Set hours
moment().subtract(1, 'hours')
-
Set minute
moment().subtract(1, 'minutes')
-
Set seconds
moment().subtract(1, 'seconds')
-
Formatting time
-
Format Time
moment().format() moment().format(String)
-
Format year, month and day:'xx XX year, XX month, XX day'
moment().format('YYYY year MM month DD day')
-
Formatting year, month and day:'xx xx-xx-xx'
moment().format('YYYY-MM-DD')
-
Format time-seconds (24-hour system):'xx time XX minutes XX seconds'
moment().format('HH time mm branch ss second')
-
Format time-seconds (12-hour system):'xx: xx:xx:xx am/pm'
moment().format('hh:mm:ss a')
-
Format timestamps (in seconds)
moment().format('X') // Return value is string type
-
Formatting timestamps (in milliseconds)
moment().format('x') // Return value is string type
-
Comparison time
-
Difference
moment().diff(Moment|String|Number|Date|Array)
-
Get the time difference between two dates
let start_date = moment().subtract(1, 'weeks') let end_date = moment() end_date.diff(start_date) // Return milliseconds end_date.diff(start_date, 'months') // 0 end_date.diff(start_date, 'weeks') // 1 end_date.diff(start_date, 'days') // 7 start_date.diff(end_date, 'days') // -7
-
Convert to JavaScript native Date object
moment().toDate() new Date(moment())
-
Converting Moment Time to JavaScript Native Date Object
let m = moment() let nativeDate1 = m.toDate() let nativeDate2 = new Date(m) String(nativeDate1) === String(nativeDate2) // true
actual combat
-
Get from yesterday 0:0:0 to yesterday 23:59:59, format: [milliseconds, milliseconds]
-
Get the time range from last Monday to last Sunday in the format: [seconds, seconds]
-
Get the time range of the first and last day of last month in format: [YYYY-MM-DD, YYYY-MM-DD]
Author: zhouqichao
Links: https://www.jianshu.com/p/9c10543420de
Source: Brief Book
The copyright of the brief book belongs to the author. For any form of reprinting, please contact the author for authorization and indicate the source.