Moment.js easy to manage date and time samples

Source: Internet
Author: User

You will encounter the problem of date and time in the front-end JavaScript development, often bring a lot of processing functions to complete a simple date time display effect. Today I'm going to introduce you to a lightweight JavaScript date-processing library: Moment.js, which you can use to easily address the various date-time issues encountered in front-end development.
View Demo Download source code
Moment.js does not rely on any third-party libraries, supports strings, dates, timestamps, and arrays, and can format date time, calculate relative time, get date time after a specific time, and so on, like PHP's date () function, which is illustrated in the following examples.
Format Date
Current time:

Moment (). Format (' Yyyy-mm-dd HH:mm:ss '); 2014-09-24 23:36:09
Today is the day of the week:

Moment (). Format (' d '); 3
To convert the current time UNIX timestamp:

Moment (). Format (' X ');
Relative time
20120901 Relative Current date is 2 years ago

Moment ("20120901", "YYYYMMDD"). Fromnow (); 2 years ago
7 days after Date:

Moment (). Add (' Days ', 7). Format (' YYYY year mm month DD Day '); October 01, 2014
9 hours after the time:

Moment (). Add (' hours ', 9). Format (' HH:mm:ss ');
Moment.js provides a rich description document that you can use to create complex date-time applications such as calendar items. The most common use of our daily development is formatting time, and I'll make a tabular description of the common format for friends who need it:

Format code Description Return value Example
M The month that the number represents, without a leading zero 1 to 12
Mm The month that the number represents, with a leading zero 01 to 12
MMM A three-letter-abbreviated month to Dec.
MMMM month, full text format January to December
Q Quarter 1 to 4
D Days of the month, no leading zeros 1 to 31
Dd The day ordinal of the month, with a leading zero 01 to 31
D The day ordinal of the week, the number indicates 0 to 6,0 says Sunday, 6 means Saturday
Ddd Three letters representing the day of the week Sun to Sat
dddd Day of the week, full week text From Sunday to Saturday
W Weeks ordinal of the year such as 42: Week 42nd
YYYY Year with four digits full representation such as: 2014 or 2000
Yy Year in two-digit digits such as: 14 or 98
A Uppercase AM PM AM PM
A lowercase am pm Am PM
HH Hour, 24-hour system with leading zeros 00 to 23
H Hour, 24-hour system, no leading zero 0 to 23
hh Hour, 12-hour system with leading zeros 00 to 12
H Hour, 12-hour system, no leading zero 0 to 12
M Number of minutes without 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 A description with a leading zero 01 to 59
X Unix time stamp 1411572969

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.