FED

©FrontEndDev.org
2015 - 2024
web@2.23.0 api@2.21.1

moment.js 全面的时间解析、转换、验证库

Parse, validate, manipulate, and display dates in JavaScript.

Install

bower install moment --save # bower
npm install moment --save   # npm
Install-Package Moment.js   # NuGet
spm install moment --save   # spm
meteor add momentjs:moment  # meteor

Format Dates

moment().format('MMMM Do YYYY, h:mm:ss a'); // February 9th 2015, 12:21:56 pm
moment().format('dddd');                    // Monday
moment().format("MMM Do YY");               // Feb 9th 15
moment().format('YYYY [escaped] YYYY');     // 2015 escaped 2015
moment().format();                          // 2015-02-09T12:21:56+08:00

Relative Time

moment("20111031", "YYYYMMDD").fromNow(); // 3 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 3 years ago
moment().startOf('day').fromNow();        // 12 hours ago
moment().endOf('day').fromNow();          // in 12 hours
moment().startOf('hour').fromNow();       // 22 minutes ago

Calendar Time

moment().subtract(10, 'days').calendar(); // 01/30/2015
moment().subtract(6, 'days').calendar();  // Last Tuesday at 12:21 PM
moment().subtract(3, 'days').calendar();  // Last Friday at 12:21 PM
moment().subtract(1, 'days').calendar();  // Yesterday at 12:21 PM
moment().calendar();                      // Today at 12:21 PM
moment().add(1, 'days').calendar();       // Tomorrow at 12:21 PM
moment().add(3, 'days').calendar();       // Thursday at 12:21 PM
moment().add(10, 'days').calendar();      // 02/19/2015

Multiple Locale Support

moment().format('L');    // 02/09/2015
moment().format('l');    // 2/9/2015
moment().format('LL');   // February 9, 2015
moment().format('ll');   // Feb 9, 2015
moment().format('LLL');  // February 9, 2015 12:21 PM
moment().format('lll');  // Feb 9, 2015 12:21 PM
moment().format('LLLL'); // Monday, February 9, 2015 12:21 PM
moment().format('llll'); // Mon, Feb 9, 2015 12:21 PM