Preface
It is a lightweight JavaScript time library, which facilitates time operation in daily development and improves development efficiency. In daily development, the following operations are usually performed on time: such as obtaining time, setting time, formatting time, comparing time, etc. The following is my compilation of the use process, which is convenient for future review.
1. Introduction
Method introduction
(1) Installation
npm install moment
oryarn add moment
(2) Introduce
// require methodvar moment = require('moment'); // import methodimport moment from 'moment';
2. Browser method introduction
<script src=""></script>
2. Set the moment area to China
// require methodrequire('moment/locale/zh-cn') ('zh-cn'); // import methodimport 'moment/locale/zh-cn' ('zh-cn');
III. Use
1. Obtain the time
(1) Get the current time
moment()
(2) Get 0:00:00:00
moment().startOf('day')
(3) Get the first day of the week (Sunday) 0:00:00
moment().startOf('week')
(4) Get 0:00:00 on Monday this week
moment().startOf('isoWeek')
(5) Get the first day of the current month 0:0:00:00:00
moment().startOf('month')
(6) Get 23:59:59 today
moment().endOf('day')
(7) Get the last day of the week (Saturday) 23:59:59
moment().endOf('week')
(8) Get this Sunday at 23:59:59
moment().endOf('isoWeek')
(9) Get the last day of the current month at 23:59:59 seconds
moment().endOf('month')
(10) Get the total number of days in the current month
moment().daysInMonth()
(11) Get the timestamp (in seconds)
moment().format('X') // The return value is string typemoment().unix() // The return value is numerical
(12) Get the timestamp (in milliseconds)
moment().format('x') // The return value is string typemoment().valueOf() // The return value is numerical
(13) Obtain year
moment().year() moment().get('year')
(14) Get Month
moment().month() // (0~11, 0: January, 11: December) moment().get('month')
(15) Get a day of the month
moment().date() moment().get('date')
(16) 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')
(17) Get hours
moment().hours() moment().get('hours')
(18) Get minutes
moment().minutes() moment().get('minutes')
(19) Get the seconds
moment().seconds() moment().get('seconds')
(20) Get the current year, month, day, hour, minute, and second
moment().toArray() // [years, months, date, hours, minutes, seconds, milliseconds] moment().toObject() // {years: xxxx, months: x, date: xx ...}
2. Set the time
(1) Set the year
moment().year(2019) moment().set('year', 2019) moment().set({year: 2019})
(2) Set month
moment().month(11) // (0~11, 0: January, 11: December) moment().set('month', 11)
(3) Set a day of a certain month
moment().date(15) moment().set('date', 15)
(4) Set a certain day of the week
moment().weekday(0) // Set the date to the first day of the week (Sunday)moment().isoWeekday(1) // Set the date to Monday this weekmoment().set('weekday', 0) moment().set('isoWeekday', 1)
(5) Set the hours
moment().hours(12) moment().set('hours', 12)
(6) Set minutes
moment().minutes(30) moment().set('minutes', 30)
(7) Set the number of seconds
moment().seconds(30) moment().set('seconds', 30)
(8) Year +1
moment().add(1, 'years') moment().add({years: 1})
(9) Month +1
moment().add(1, 'months')
(10) Date +1
moment().add(1, 'days')
(11) Week + 1
moment().add(1, 'weeks')
(12) hours + 1
moment().add(1, 'hours')
(13) Minutes + 1
moment().add(1, 'minutes')
(14) Seconds +1
moment().add(1, 'seconds') #### (15) Year -1```js moment().subtract(1, 'years') moment().subtract({years: 1})
(16) Month -1
moment().subtract(1, 'months')
(17) Date-1
moment().subtract(1, 'days')
(18) Weeks-1
moment().subtract(1, 'weeks')
(19) hours-1
moment().subtract(1, 'hours')
(20) Minutes-1
moment().subtract(1, 'minutes')
(21) Seconds -1
moment().subtract(1, 'seconds')
3. Format time
Format code | illustrate | Return value example |
---|---|---|
M | Months represented by numbers, without leading zeros | 1 to 12 |
MM | Months represented by numbers, with leading zeros | 01 to 12 |
MMM | The month represented by the three-letter abbreviation | Jan to Dec |
MMMM | Month, full text format | January to December |
Q | Quarterly | 1 to 4 |
D | What day of the month, no leading zeros | 1 to 31 |
DD | What day of the month has leading zeros | 01 to 31 |
d | What day of the week, the numbers indicate | 0 to 6, 0 means Sunday, 6 means Saturday |
ddd | Three letters indicate the day of the week | Sun to Sat |
dddd | Day of the week, full text of the week | From Sunday to Saturday |
w | What week of year | Such as 42: indicates week 42 |
YYYY | The year fully represented by four digits | For example: 2014 or 2000 |
YY | Years represented by two digits | For example: 14 or 98 |
A | Uppercase AM PM | AM PM |
a | lowercase am pm | am pm |
HH | Hours, 24-hour system, leading zeros | 00 to 23 |
H | Hours, 24-hour system, no leading zeros | 0 to 23 |
hh | Hour, 12-hour system, leading zero | 00 to 12 |
h | Hours, 12-hour system, no leading zeros | 0 to 12 |
m | No leading zeros | 0 to 59 |
mm | Number of minutes with leading zeros | 00 to 59 |
s | Number of seconds without leading zeros | 1 to 59 |
ss | Description of leading zeros | 01 to 59 |
X | Unix timestamp | 1411572969 |
(1) Format year, month and day: 'xxx year, xxx month and xx day'
moment().format('YYYYY MM month DD day')
(2) Format year, month and day: 'xxxx-xx-xx'
moment().format('YYYY-MM-DD')
(3) Format hours, minutes and seconds (24-hour system): 'xx hours, xx minutes, xx seconds'
moment().format('HH hour mm minute ss seconds')
(4) Format hours, minutes and seconds (12-hour system): 'xx:xx:xx am/pm'
moment().format('hh:mm:ss a')
(5) Format the timestamp (in milliseconds)
moment().format('x') // The return value is string type
4. Compare time
(1) Get the time difference between two dates
let start_date = moment().subtract(1, 'weeks') let end_date = moment() end_date.diff(start_date) // Return the number of 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
5. Convert to JavaScript native Date object
moment().toDate() new Date(moment())
6. Date formatting
Output instance
moment().format('MMMM Do YYYY, h:mm:ss a'); // May 24 2019, 7:47:43 at nightmoment().format('dddd'); // Fridaymoment().format("MMM Do YY"); // May 24 19moment().format('YYYY [escaped] YYYY'); // 2019 escaped 2019 moment().format(); // 2019-05-24T19:47:43+08:00
7. Relative time
Output instance
moment("20111031", "YYYYMMDD").fromNow(); // 8 years agomoment("20120620", "YYYYMMDD").fromNow(); // 7 years agomoment().startOf('day').fromNow(); // 20 hours agomoment().endOf('day').fromNow(); // Within 4 hoursmoment().startOf('hour').fromNow(); // 1 hour ago
8. Calendar time
Output instance
moment().subtract(10, 'days').calendar(); // May 14, 2019moment().subtract(6, 'days').calendar(); // Last Saturday at 7:49 pmmoment().subtract(3, 'days').calendar(); // This Tuesday at 7:49 pmmoment().subtract(1, 'days').calendar(); // Last night at 7:49 pmmoment().calendar(); // Tonight at 7:49moment().add(1, 'days').calendar(); // Tomorrow at 7:49 pmmoment().add(3, 'days').calendar(); // Next Monday at 7:49 pmmoment().add(10, 'days').calendar(); // June 3, 2019
9. Multilingual support
Output instance
moment().format('L'); // 2019-05-24 moment().format('l'); // 2019-05-24 moment().format('LL'); // May 24, 2019moment().format('ll'); // May 24, 2019moment().format('LLL'); // May 24, 2019 at 7:50 pmmoment().format('lll'); // May 24, 2019 at 7:50 pmmoment().format('LLLL'); // Friday, May 24, 2019 at 7:50 pmmoment().format('llll'); // Friday, May 24, 2019 at 7:50 pm
10. Other practical skills
Output instance:
moment().format("YYYY-MM-DD") // Format the current time`${moment().subtract("month", +1).format("YYYY-MM")}-01` // The 1st of the previous month`${moment().add("month", -1).format("YYYY-MM")}-01` // It was the first day of the previous monthlet M = `${moment().format("YYYY-MM")}-01` //On the first day of this monthmoment(M).add("days", -1).format("YYYY-MM-DD") // At the end of the previous monthmoment().startOf("year").format("YYYY-MM-DD") // The start date of this year, ("2019-01-01")moment().endOf("year").format("YYYY-MM-DD") // The end date of this year, ("2019-12-31")// moment to time stampmoment().valueOf() // Timestamp to momentmoment(string).format() // Solve the problem of time zone difference in Moment formatting time// `utcOffset()` receives digits, time offset, unit: minutes// Beijing time in East Eighth District, 8 hours (480 minutes) earlier than the zero-time zone, so 480 minutes should be addedMoment(date).utcOffset(480).format('YYYY-MM-DD HH:mm:ss');
For more information, please refer to the official documentation:Chinese website | Development Documents
Summarize
This is the end of this article about the summary of usage methods. For more relevant usage methods, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!