From: Tim Wood
<script src="moment.js"></script>
moment().add('hours', 1).fromNow(); // "1 hour ago"
+
+
Instead of modifying the native Date.prototype, Moment.js creates a wrapper for the Date object
To get this wrapper object, simply call moment() with one of the supported input types
A native Javascript Date object.
++
+var day = new Date(2011, 9, 16); +var dayWrapper = moment(day); + +var otherDay = moment(new Date(2020, 3, 7)); ++ +
This is the fastest way to get a Moment.js wrapper.
+ +An integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.
++
+var day = moment(1318781876406); ++ + +
A string that can be parsed by Date.parse.
+
+var day = moment("Dec 25, 1995");
+
+
+ Browser support for this is somewhat inconsistent. If you are not getting consistent results, you can try using String + Format
+ +An input string and a format string
++
+var day = moment("12-25-1995", "MM-DD-YYYY");
+
+
+ The format parsing tokens are similar to the tokens for moment.fn.format.
The parser ignores non-alphanumeric characters, so both moment("12-25-1995", "MM-DD-YYYY") and
+ moment("12\25\1995", "MM-DD-YYYY") will return the same thing.
+
| Input | +Output | +
|---|---|
| M or MM | +Month | +
| D or DD | +Day of month | +
| DDD or DDDD | +Day of year | +
| YY | +2 digit year (if greater than 70, will return 1900's, else 2000's) | +
| YYYY | +4 digit year | +
| a or A | +AM/PM | +
| H, HH, h, or hh | +24 hour (for 12 hour time, use in conjunction with a or A) | +
| m or mm | +Minutes | +
| s or ss | +Seconds | +
Important: Parsing a string with a format is by far the slowest method of creating a date. If you have the ability to change the input, it is much faster (~15x) to use Unix timestamps.
+ +An input string and an array of format strings.
+This is the same as String + Format, only it will try to match the input to multiple formats.
++
+var day = moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);
+
+
+ This approach is fundamentally problematic in cases like the following.
++
+var day = moment("05-06-1995", ["MM-DD-YYYY", "DD-MM-YYYY"]); // June 5th or May 6th?
+
+
+ Important: THIS IS SLOW. This should only be used as a last line of defense.
+ +To get the current time, just call moment() with no parameters.
+
+var now = moment(); ++ +
This is essentially the same as calling moment(new Date()).
An array mirroring the parameters passed into + Date.UTC(). +
++
+// [year, month = 0, date = 1, hours = 0, minutes = 0, seconds = 0, milliseconds = 0] +var day = moment([2010, 1, 14, 15, 25, 50, 125]); // February 14th, 3:25:50.125 PM ++ +
Any value past the year is optional, and will default to the lowest possible number.
++
+var day = moment([2010]); // January 1st +var day = moment([2010, 6]); // July 1st +var day = moment([2010, 6, 10]); // July 10th ++ + +
Once you have a Moment.js wrapper object, you may want to manipulate it in some way. There are a number of moment.fn methods to help with this.
All manipulation methods are chainable, so you can do crazy things like this.
++
+moment().add('days', 7).subtract('months', 1).year(2009).hours(0).minutes(0).seconds(0);
+
+
+
+ This is a pretty robust function for adding time to an existing date. To add time, pass the key of what time you want to add, and the amount you want to add.
++
+moment().add('days', 7);
+
+
+ There are some shorthand keys as well if you're into that whole brevity thing.
++
+moment().add('d', 7);
+
+
+ | Key | +Shorthand | +
|---|---|
| years | +y | +
| months | +M | +
| days | +d | +
| hours | +h | +
| minutes | +m | +
| seconds | +s | +
| milliseconds | +ms | +
If you want to add multiple different keys at the same time, you can pass them in as an object literal.
++
+moment().add('days', 7).add('months', 1); // with chaining
+moment().add({days:7,months:1}); // with object literal
+
+
+ There are no upper limits for the amounts, so you can overload any of the parameters.
++
+moment().add('milliseconds', 1000000); // a million milliseconds
+moment().add('days', 360); // 360 days
+
+
+ If the day of the month on the original date is greater than the number + of days in the final month, the day of the month will change to the last + day in the final month.
+Example:
++
+moment([2010, 0, 31]); // January 31
+moment([2010, 0, 31]).add('months', 1); // February 28
+
+
+
+ This is exactly the same as moment.fn.add, only instead of adding time, it subtracts time.
+
+moment().subtract('days', 7);
+
+
+
+ There are a number of shortcut getter and setter functions. Much like in jQuery, calling the function without paremeters causes it to function as a getter, and calling with a parameter causes it to function as a setter.
+These map to the corresponding function on the native Date object.
+
+moment().seconds(30) === new Date().setSeconds(30); +moment().seconds() === new Date().getSeconds(); ++ + +
+
+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 ++ + +
+
+moment().day(5); // set the day to 5 +moment().day(); // get the day value ++ + +
+
+moment().month(5); // set the month to June +moment().month(); // get the month value ++ + +
+
+moment().year(1984); // set the year to 1984 +moment().year(); // get the year value