插件

back

Some other people have made plugins for Moment.js that may be useful to you.

Strftime

npm install moment-strftime

If you are more comfortable working with strftime instead of LDML-like parsing tokens, you can use Ben Oakes' plugin moment-strftime.

The repository is located at https:///github.com/benjaminoakes/moment-strftime

ISO Calendar

npm install moment-isocalendar

If you are looking for a Python-like isocalendar method, you can use Rocky Meza's plugin

moment-isocalendar

Calling the isocalendar method on a moment will return an array like the following:

[year, week_of_year, day_of_week, minutes_since_midnight]

moment().isocalendar(); // [2012, 8, 5, 870]

You can also reconstruct a moment from a isocalendar array.

moment.fromIsocalendar([2011, 51, 5, 870]).format('LLLL');
// "Friday, December 23 2011 2:30 PM"

The repository is located at https://github.com/fusionbox/moment-isocalendar

Date Ranges

npm install moment-range

If you need to work with date ranges, you can use Gianni Chiappetta's plugin moment-range.

Documentation can be found on the homepage https://github.com/gf3/moment-range.

And it is also available for the web at the repository below.

The repository is located at https://github.com/gf3/moment-range

Twix

npm install twix

Another range plugin is Isaac Cambron's library Twix. It has many range-related features and excels at formatting ranges readably. For example,

var t = moment("1/25/1982 9:30 AM").twix("1/25/1982 1:30 PM");
t.isCurrent(); // false
t.count('minutes'); // 241
t.format();  // 'Jan 25, 1982, 9:30 AM - 1:30 PM'
t.simpleFormat("h:m"); // '9:30 - 1:30'

Full documentation of all the options and features is here.

It's available on npm like so:

npm install twix

Or just grab the JS file from here.

Twitter

If you're trying to format times for tweets like the way Twitter does, you can use the moment.twitter plugin by @hijonathan.

It's a simple way to display both short and long versions of human-readable timestamps.

moment().subtract(5, 'hours').twitterLong();
// 5 hours

Yes, it does smart pluralization.

moment().subtract(1, 'hour').twitterLong();
// 1 hour

Not short enough for you?

moment().subtract(6, 'days').twitterShort();
// 6d

Jalaali Calendar

npm install moment-jalaali

If you want to work with Jalaali calendar system (Jalali, Persian, Khorshidi or Shamsi), you can use Behrang Noruzi Niya's plugin moment-jalaali.

When installed, it will wrap moment and moment will be able to format and parse Jalaali years and months. Here is a short example:

var m = moment('1360/5/26', 'jYYYY/jM/jD'); // Parse a Jalaali date.
m.format('jYYYY/jM/jD [is] YYYY/M/D'); // 1360/5/26 is 1981/8/17

The repository is located at github.com/behrang/moment-jalaali.

MSDate

If you are using OLE Automation dates in .NET check out Markit On Demand's moment-msdate. Using this plugin allows you to format OA dates into JavaScript dates and vice-versa.

Convert a moment to an OA date:

moment().toOADate(); // a floating point number

Or, convert an OA date to a moment:

moment.fromOADate(41493); // Wed Aug 07 2013 00:00:00 GMT-0600 (MDT)

More information and detailed docs can be found on GitHub at http://markitondemand.github.io/moment-msdate/.

Fiscal Quarters

If you ever have need for Fiscal, Calendar or Academic quarters, you can use the moment-fquarter plugin by @robgallen.

At its simplest, just call the fquarter method on any moment object. It returns a formatted string with April being the first quarter.

moment("2013-01-01").fquarter();
// Q4 2012/13

You can pass in any month as the starting quarter, e.g. July

moment("2013-01-01").fquarter(7);
// Q3 2012/13

If you want calendar quarters, start in January

moment("2013-01-01").fquarter(1);
// Q1 2013

Precise Range

The Precise Range plugin, written by Rob Dawson, can be used to display exact, human-readable representations of date/time ranges

moment("2014-01-01 12:00:00").preciseDiff("2015-03-04 16:05:06");
 // 1 year 2 months 3 days 4 hours 5 minutes 6 seconds
moment.preciseDiff("2014-01-01 12:00:00", "2014-04-20 12:00:00");
// 3 months 19 days

Recur

npm install moment-recur

If you need to work with recurring dates, you can use Casey Trimm's plugin moment-recur.

This plugin will allow you to create length-based intervals (days, weeks, etc.) and calendar-based intervals (daysOfMonth, monthsOfYear, etc.).

It provides a matches function to test whether a date recurs according to the rules set, as well as generator functions to get the next and previous dates in a series.

The repository, documentation, and many more examples can be found at https://github.com/c-trimm/moment-recur

var interval = moment( "01/01/2014" ).recur().every(2).days(); // Length Interval
interval.matches( "01/03/2014" ); // true
interval.next( 2, "L" ); // ["01/03/2014", "01/05/2014"]
interval.forget( "days" ); // Remove a rule
interval.dayOfMonth( 10 ); // Calendar Interval
interval.matches( "05/10/2014" ); // true
interval.previous( 2, "L" ); // ["12/10/2013", "11/10/2013"]

Parse Date Format

npm install moment-parseformat

This plugin extracts the format of a date/time string.

var format = moment.parseFormat('Thursday, February 6th, 2014 9:20pm');
// dddd, MMMM Do, YYYY h:mma
moment().format(format); // format

That allows to create smart date inputs that let your users set a Date/Time and lets you extract the user's preferred format for future usage. Find an example usage of it at minutes.io.

The Plugin has been authored by @gr2m. Links: Demo | Source

Java DateFormat Parser

npm install moment-jdateformatparser

If you want to work with the java.text.DateFormat you can use this plugin.

For example,

moment("2013-12-24 14:30").formatWithJDF("dd.MM.yyyy");  // returns the formatted date "24.12.2013"
moment().toJDFString("DD.MM.YYYY");  // returns the Java format pattern "dd.MM.yyyy"

The repository is located at github.com/MadMG/moment-jdateformatparser

Hijri Calendar

npm install moment-hijri

If you want to work with Hijri calendar then you can use moment-hijri plugin. moment-hijri is a moment plugin for the Hijri lunar calendar based on Umm al-Qura calculations. This plugin is developed by Suhail Alkowaileet.
如果要使用回历日历,则可以使用moment-hijri插件。 moment-hijri是基于Umm al-Qura计算得出的Hijri阴历的矩插件。该插件由Suhail Alkowaileet开发。

When you install it, it will wrap moment and you will be able to parse Hijri dates. Here is a short example:

m = moment('1410/8/28', 'iYYYY/iM/iD'); // Parse a Hijri date.
m.format('iYYYY/iM/iD [is] YYYY/M/D'); // 1410/8/28 is 1990/3/25

The repository is located at https://github.com/xsoh/moment-hijri.

Transform

bower install moment-transform

moment-transform is a plugin that manipulated dates through patterns. You can use basic operations –set/add/substract– on individual parts (hours, month, …) of a Moment instance.

moment().transform('YYYY-MM-+01 00:00:00.000'); // Tonight at midnight
moment().transform('14:30:00.000'); // Today, 2:30 pm
moment().transform('YYYY-MM--30 00:00:00.000'); // 30 days ago

Optional parameters lets you specify custom patterns and force strict pattern usage (non-alphabetic characters are not mandatory in passed string by default).

moment().transform('+01MMYYYY', 'DD/MM/YYYY', false); // Tomorrow, same time
moment().transform('+01MMYYYY', 'DD/MM/YYYY', true); // Invalid date

You can see it live there while the repository is here.