From 4122959e04efe4c09e9faa7bf293d9e9c18f9f58 Mon Sep 17 00:00:00 2001
From: Tim Wood
moment().minutes(30); // set the minutes to 30
-moment().minutes(); // get the minutes value
moment().hours(12); // set the hours to 12 -moment().hours(); // get the hours value@@ -355,7 +353,6 @@ moment().hours(); // get the hours value
moment().day(5); // set the day to 5 -moment().day(); // get the day value@@ -363,7 +360,6 @@ moment().day(); // get the day value
moment().month(5); // set the month to June -moment().month(); // get the month value@@ -371,7 +367,291 @@ moment().month(); // get the month value
moment().year(1984); // set the year to 1984 -moment().year(); // get the year value ++ + +
Once parsing and manipulation are done, you need some way to display the moment. Moment.js offers many ways of doing that.
+ +The most robust display option is moment.fn.format. It takes a string of tokens and replaces them with their corresponding values from the Date object.
+
+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 | +EST CST ... MST PST | +
Another common way of displaying time, sometimes called timeago, is handled by moment.fn.from.
+
+var a = moment([2007, 0, 29]); +var b = moment([2007, 0, 28]); +a.from(b) // "a day ago" ++ +
If you pass true as the second parameter, you can get the value without the suffix. This is useful wherever you need to have a human readable length of time.
+
+var start = moment([2007, 0, 5]); +var end = moment([2007, 0, 10]); +start.from(end); // "in 5 days" +start.from(end, true); // "5 days" ++ +
The base strings are customized by moment.lang or by modifying the values directly using moment.relativeTime.
The breakdown of which string is displayed when is outlined in the table below.
+| Range | +Key | +Sample Output | +
|---|---|---|
| 0 to 45 seconds | +s | +seconds ago | +
| 45 to 90 seconds | +m | +a minute ago | +
| 90 seconds to 45 minutes | +mm | +2 minutes ago ... 45 minutes ago | +
| 45 to 90 minutes | +h | +an hour ago | +
| 90 minutes to 22 hours | +hh | +2 hours ago ... 22 hours ago | +
| 22 to 36 hours | +d | +a day ago | +
| 36 hours to 25 days | +dd | +2 days ago ... 25 days ago | +
| 25 to 45 days | +M | +a month ago | +
| 45 to 345 days | +MM | +2 months ago ... 11 months ago | +
| 345 to 547 days (1.5 years) | +y | +a year ago | +
| 548 days+ | +yy | +2 years ago ... 20 years ago | +
This is just a map to moment.fn.from(new Date())
+
+moment([2007, 0, 29]).fromNow(); // 4 years ago ++ +
Like moment.fn.from, if you pass true as the second parameter, you can get the value without the suffix.
+
+moment([2007, 0, 29]).fromNow(); // 4 years ago +moment([2007, 0, 29]).fromNow(true); // 4 yearsdiff --git a/package.json b/package.json index c06bfc781..83144272e 100755 --- a/package.json +++ b/package.json @@ -1,25 +1,27 @@ { - "name": "underscore.date", - "version": "0.6.1", - "description": "Underscore.date is a javascript date library that helps create, manipulate, and format dates without extending the `Date` prototype.", - "homepage": "https://github.com/timrwood/underscore.date", - "author": "Tim Wood