From: Tim Wood
var date = new Date(2010, 1, 14, 15, 25, 50, 125);
moment(date).format("dddd, MMMM Do YYYY, h:mm:ss a"); // "Sunday, February 14th 2010, 3:25:50 pm"
moment(date).format("ddd, hA"); // "Sun, 3PM"
-| Token | Output |
|---|---|
| Month | |
| M | 1 2 ... 11 12 |
| Mo | 1st 2nd ... 11th 12th |
| MM | 01 02 ... 11 12 |
| MMM | Jan Feb ... Nov Dec |
| MMMM | January February ... November December |
| Day of Month | |
| D | 1 2 ... 30 30 |
| Do | 1st 2nd ... 30th 31st |
| DD | 01 02 ... 30 31 |
| Day of Year | |
| DDD | 1 2 ... 364 365 |
| DDDo | 1st 2nd ... 364th 365th |
| DDDD | 001 002 ... 364 365 |
| Day of Week | |
| d | 0 1 ... 5 6 |
| do | 0th 1st ... 5th 6th |
| ddd | Sun Mon ... Fri Sat |
| dddd | Sunday Monday ... Friday Saturday |
| Week of Year | |
| w | 1 2 ... 52 53 |
| wo | 1st 2nd ... 52nd 53rd |
| ww | 01 02 ... 52 53 |
| Year | |
| YY | 70 71 ... 29 30 |
| YYYY | 1970 1971 ... 2029 2030 |
| AM/PM | |
| A | AM PM |
| a | am pm |
| Hour | |
| H | 0 1 ... 22 23 |
| HH | 00 01 ... 22 23 |
| h | 1 2 ... 11 12 |
| hh | 01 02 ... 11 12 |
| Minute | |
| m | 0 1 ... 58 59 |
| mm | 00 01 ... 58 59 |
| Second | |
| s | 0 1 ... 58 59 |
| ss | 00 01 ... 58 59 |
| Timezone | |
| z or zz (lowercase) | EST CST ... MST PST |
| Z | -07:00 -06:00 ... +06:00 +07:00 |
| ZZ | -0700 -0600 ... +0600 +0700 |
| Localized date format | |
| L | 07/10/1986 |
| LL | July 10 1986 |
| LLL | July 10 1986 8:30 PM |
| LLLL | Saturday, July 10 1986 8:30 PM |
While these date formats are very similar to LDML date formats, there are a few minor differences regarding day of month, day of year, and day of week.
For a breakdown of a few different date formatting tokens, see this chart of date formatting tokens.
To compare moment.js date formatting speeds against other libraries, check out http://jsperf.com/date-formatting . +
| Token | Output |
|---|---|
| Month | |
| M | 1 2 ... 11 12 |
| Mo | 1st 2nd ... 11th 12th |
| MM | 01 02 ... 11 12 |
| MMM | Jan Feb ... Nov Dec |
| MMMM | January February ... November December |
| Day of Month | |
| D | 1 2 ... 30 30 |
| Do | 1st 2nd ... 30th 31st |
| DD | 01 02 ... 30 31 |
| Day of Year | |
| DDD | 1 2 ... 364 365 |
| DDDo | 1st 2nd ... 364th 365th |
| DDDD | 001 002 ... 364 365 |
| Day of Week | |
| d | 0 1 ... 5 6 |
| do | 0th 1st ... 5th 6th |
| ddd | Sun Mon ... Fri Sat |
| dddd | Sunday Monday ... Friday Saturday |
| Week of Year | |
| w | 1 2 ... 52 53 |
| wo | 1st 2nd ... 52nd 53rd |
| ww | 01 02 ... 52 53 |
| Year | |
| YY | 70 71 ... 29 30 |
| YYYY | 1970 1971 ... 2029 2030 |
| AM/PM | |
| A | AM PM |
| a | am pm |
| Hour | |
| H | 0 1 ... 22 23 |
| HH | 00 01 ... 22 23 |
| h | 1 2 ... 11 12 |
| hh | 01 02 ... 11 12 |
| Minute | |
| m | 0 1 ... 58 59 |
| mm | 00 01 ... 58 59 |
| Second | |
| s | 0 1 ... 58 59 |
| ss | 00 01 ... 58 59 |
| Timezone | |
| z or zz (lowercase) | EST CST ... MST PST |
| Z | -07:00 -06:00 ... +06:00 +07:00 |
| ZZ | -0700 -0600 ... +0600 +0700 |
| Localized date format | |
| L | 07/10/1986 |
| LL | July 10 1986 |
| LLL | July 10 1986 8:30 PM |
| LLLL | Saturday, July 10 1986 8:30 PM |
To escape characters in format strings, you can use a backslash before any character. NOTE: if you are using a string literal, you will need to escape the backslash, hence the double backslash below.
moment().format('\\L'); // outputs 'L'To escape multiple characters, you can wrap the characters in square brackets.
moment().format('[today] DDDD'); // 'today Sunday'While these date formats are very similar to LDML date formats, there are a few minor differences regarding day of month, day of year, and day of week.
For a breakdown of a few different date formatting tokens, see this chart of date formatting tokens.
To compare moment.js date formatting speeds against other libraries, check out http://jsperf.com/date-formatting .
Note: Baron Schwartz wrote a pretty cool date formatter that caches formatting functions to avoid expensive regex or string splitting. It's so much faster than any of the other libraries, that it's best to compare it on its own, rather than pollute the "best of the uncompiled" formatting libs.
Here's the moment.js vs xaprb performance tests, and here is the article describing his methods.
Another common way of displaying time, sometimes called timeago, is handled by moment.fn.from.
var a = moment([2007, 0, 29]);
diff --git a/sitesrc/docs.jade b/sitesrc/docs.jade
index 178f640fe..2d167ee3e 100644
--- a/sitesrc/docs.jade
+++ b/sitesrc/docs.jade
@@ -646,6 +646,10 @@ block content
tr
td LLLL
td Saturday, July 10 1986 8:30 PM
+ p To escape characters in format strings, you can use a backslash before any character. NOTE: if you are using a string literal, you will need to escape the backslash, hence the double backslash below.
+ pre moment().format('\\\\L'); // outputs 'L'
+ p To escape multiple characters, you can wrap the characters in square brackets.
+ pre moment().format('[today] DDDD'); // 'today Sunday'
p While these date formats are very similar to LDML date formats, there are a few minor differences regarding day of month, day of year, and day of week.
p For a breakdown of a few different date formatting tokens, see
a(href="https://docs.google.com/spreadsheet/ccc?key=0AtgZluze7WMJdDBOLUZfSFIzenIwOHNjaWZoeGFqbWc&hl=en_US#gid=0") this chart of date formatting tokens.