Get the App
SLTechnology News&Howtos  ›  Development  › 

Case Analysis of Common usage of Moment.js

Shulou Source: shulou.com Published: 2022-05-31 18:31:34 10月02日 Update

This article mainly introduces the relevant knowledge of "Moment.js common usage case analysis". The editor shows you the operation process through the actual case, and the operation method is simple, fast and practical. I hope this article "Moment.js common usage case analysis" can help you solve the problem.

Moment.js is a lightweight js time processing class library, which is easy to use, facilitates the operation of time in daily development, and improves the development efficiency.

Quote the commonly used method 1 of Moment.jsnpm install moment, moment ()

Get the current date and time

Moment ()

Get the date and time of the String

Moment (String) 2. Get get

Get the year of the day

Moment () .get ('year')

Get the month 0-11 of the day

Moment () .get ('month')

Get the date of the day

Moment () .get ('date') 3, format format

The resulting time format is YYYY-MM-DD

Moment (String,'YYYY-MM-DD') moment (String) .format ('YYYY-MM-DD') 4, set subtract

.subtract (Number, String)

Set the year, get the time of one year ago

Moment () .subtract (1 'years')

Set the month to get the time of one month ago

Moment () .subtract (1 'months')

Set the date to get the time of yesterday

Moment () .subtract (1, 'days') 5, start startOf ()

Change the original moment by setting it to the beginning of the time unit.

.startOf (String)

Get today's 00:00:00

Moment () .startOf ('day')

Get 00:00:00 on the first day of the week

Moment () .startOf ('week') 6, end endOf ()

Change the original moment by setting it to the end of the time unit

.endOf (String)

Get today's 23:59:59

Moment () .endOf ('day')

Get 23:59:59 on the first day of the week

Moment () .endOf ('week') 7, total days Days in Month

.daysInMonth ()

Gets the number of days in February.

Moment ("2012-02", "YYYY-MM"). DaysInMonth () / / 298, timestamp

.unix () / / seconds

.valueof () / / milliseconds

Get timestamp (in seconds)

Moment (). Format ('X'). Unix () / the return value is numeric

Get timestamp (in milliseconds)

Moment (). Format ('x'). ValueOf () / / the return value is numeric 9, the actual combat on the ant selection time

The disabledDate use of the a-range-picker component of ant

You cannot choose a date before today (including today)

DisabledDate (current) {return current & & current < moment () .endOf ('day');}

You cannot choose a date before today (excluding today)

DisabledDate (current) {return current & & current < moment (). Subtract (1, 'days'). EndOf (' day')}

Click on the selected data before 2019-01-01 cannot be confirmed.

DisabledDate (current) {return current & & current < moment ('2019-01-01')}, that's all for the analysis of examples of common uses of Moment.js. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Time date unit selection instance case analysis common analysis knowledge original days year numerical value method month format industry development different practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Shulou Technology Microsoft MySQL