Moment.js Awesome JavaScript date processing class Library

Source: Internet
Author: User
Tags month name

Moment.js not to miss the awesome JavaScript date processing class Library

Key Features:

    1. 3.2KB Ultra Lightweight
    2. Independent class library, meaning that you don't have to pour a bunch of JS
    3. Date processing supports UNIX timestamp, String, date of specified format
    4. Date processing: Plus, minus date
    5. Date display: Includes date display options for relative time display
    6. Other built-in features, such as Save, timezone offset and i18n support
    7. Can act as a module for node. js

Use:

First, you can create a new moment object. It is done through the global Moment () method. If you do not set the parameter, it will use the current time.

Create a Moment object
 1  //  2  var  now = Moment ();  3  //   Create a moment in the past, using a string date  5  var  m = Moment ("April 1st, 2005", "Mmm-dd-yyyy" ); 6  //   Create a new moment using an array  8     var  m = Moment ([2005, 3, 1]); 

Note: As with the JavaScript Date () object, the month is starting at 0, so 3 is April.

Format time
1 //What is it?2Console.log (Moment (). Format (' HH:mm:ss '));//16:13:113  4 //What's the week is it?5 varDay = Moment (). Day ();//56Console.log (Moment.weekdays[day]);//Friday7  8 //What's the current month name?9Console.log (Moment.months[moment (). month ()]);//AugustTen   One //What's it in London? (Time ZONE:UTC) AConsole.log (MOMENT.UTC (). Format (' HH:mm:ss '));//13:23:41 -   - //What's it in Japan? (Time zone:utc+9) theConsole.log (MOMENT.UTC (). Add (' hours ', 9). Format (' HH:mm:ss '));//22:23:41
View CodeFormatted date
1 //How is old is you ?2 varM = Moment ("Mar 26th, 1989", "Mmm-dd-yyyy");3  4Console.log (' You is ' +m.fromnow () + ' old ');//You are all years ago old5  6 //Oops. We better leave the "ago" part out:7Console.log (' You Are ' +m.fromnow (true) + ' old ');//You are at years old8  9 //When would the next World Cups be?TenConsole.log (Moment (' June 12th, ', ' MMM DD YYYY '). Fromnow ());//In 2 years One   A //What would be is the date 7 days from now? -Console.log (Moment (). Add (' Days ', 7). Format (' MMMM do, YYYY ');//September 7th,
View Code

Moment.js Awesome JavaScript date processing class Library

Related Article

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.