Detailed explanation of moment.js time and date processing

Detailed explanation of moment.js time and date processing

Monday to Sunday time format conversion

(Y --- year M --- month D--- day)

 
    : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : : :
// Get the total number of days in the current month moment().daysInMonth()

Excerpt from the official website:

Date formatting

moment().format('MMMM Do YYYY, h:mm:ss a'); // May 27, 2020, 4:47:31 p.m. moment().format('dddd'); // Wednesday moment().format("MMM Do YY"); // May 27, 2020, 4:47:31 p.m.
moment().format('YYYY [escaped] YYYY'); // 2020 escaped 2020
moment().format(); // 2020-05-27T16:47:31+08:00

Relative time

moment("20111031", "YYYYMMDD").fromNow(); // 9 years ago moment("20120620", "YYYYMMDD").fromNow(); // 8 years ago moment().startOf('day').fromNow(); // 17 hours ago moment().endOf('day').fromNow(); // 7 hours ago moment().startOf('hour').fromNow(); // 1 hour ago

Calendar time

moment().subtract(10, 'days').calendar(); // 2020/05/17
moment().subtract(6, 'days').calendar(); // Last Thursday 16:47
moment().subtract(3, 'days').calendar(); // Last Sunday 16:47
moment().subtract(1, 'days').calendar(); // Yesterday 16:47
moment().calendar(); // Today 16:47
moment().add(1, 'days').calendar(); // Tomorrow 16:47
moment().add(3, 'days').calendar(); // Next Saturday 16:47
moment().add(10, 'days').calendar(); // 2020/06/06

Multi-language support

moment.locale(); // zh-cn
moment().format('LT'); // 16:47
moment().format('LTS'); // 16:47:31
moment().format('L'); // 2020/05/27
moment().format('l'); // 2020/5/27
moment().format('LL'); // May 27, 2020 moment().format('ll'); // May 27, 2020 moment().format('LLL'); // May 27, 2020 4:47 PM moment().format('lll'); // May 27, 2020 16:47
moment().format('LLLL'); // Wednesday, May 27, 2020, 4:47 PM moment().format('llll'); // Wednesday, May 27, 2020, 16:47

I have compiled a small number of commonly used ones, and I want to learn more about their use. Below are two commonly used link addresses for reference only

1. moment.js Address: https://github.com/moment/moment
2. Front-end open source project CDN acceleration service, basically all open source js can be found on it: https://www.bootcdn.cn/

This is the end of this article about moment.js-Detailed explanation of time and date processing. For more relevant moment.js-time and date processing content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Moment.js is an awesome Javascript date processing library that you shouldn't miss.
  • moment.js easily implements getting the current date is the first week of the year
  • Moment.js implements multiple simultaneous countdowns
  • Summary of common usage of Moment.js

<<:  Analysis of the usage of process control functions/statistical functions/grouping queries in MySql

>>:  Webservice remote debugging and timeout operation principle analysis

Recommend

Solution to docker suddenly not being accessible from the external network

According to the methods of the masters, the caus...

Use js to call js functions in iframe pages

Recently, I have been working on thesis proposals ...

Analysis of the use and principle of Docker Swarm cluster management

Swarm Cluster Management Introduction Docker Swar...

jQuery simulates picker to achieve sliding selection effect

This article shares the specific code of jQuery t...

How to choose transaction isolation level in MySQL project

introduction Let's start with our content. I ...

MySQL Learning: Three Paradigms for Beginners

Table of contents 1. Paradigm foundation 1.1 The ...

Vue Element front-end application development table list display

1. List query interface effect Before introducing...

How to generate mysql primary key id (self-increment, unique and irregular)

Table of contents 1. Use the uuid function to gen...

MySQL 8.0.21 installation and configuration method graphic tutorial

Record the installation and configuration method ...

How to obtain a permanent free SSL certificate from Let's Encrypt in Docker

1. Cause The official cerbot is too annoying. It ...

Nginx try_files directive usage examples

Nginx's configuration syntax is flexible and ...

MySQL horizontal and vertical table conversion operation implementation method

This article uses examples to illustrate how to i...

How to change the character set encoding to UTF8 in MySQL 5.5/5.6 under Linux

1. Log in to MySQL and use SHOW VARIABLES LIKE &#...

Analyze the compilation and burning of Linux kernel and device tree

Table of contents 1. Prepare materials 2. Downloa...