Moment.js not to miss the awesome JavaScript date processing class Library
Key Features:
- 3.2KB Ultra Lightweight
- Independent class library, meaning that you don't have to pour a bunch of JS
- Date processing supports UNIX timestamp, String, date of specified format
- Date processing: Plus, minus date
- Date display: Includes date display options for relative time display
- Other built-in features, such as Save, timezone offset and i18n support
- 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