From: Tim Wood Date: Sun, 16 Oct 2011 17:13:05 +0000 (-0700) Subject: Adding docs for parsing and manipulation X-Git-Tag: 1.0.1~11 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d34a8f80901132b7d1d3bf21de5ecbd7ba44b545;p=thirdparty%2Fmoment.git Adding docs for parsing and manipulation --- diff --git a/docs/index.html b/docs/index.html index 19a7e36a6..c9e16d97f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -96,6 +96,282 @@ moment().add('hours', 1).fromNow(); // "1 hour ago"
 <script src="moment.js"></script>
 moment().add('hours', 1).fromNow(); // "1 hour ago"
+
+

+ +

Parsing

+

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

+ +

Javascript Date Object

+

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.

+ +

Unix Timestamp

+

An integer value representing the number of milliseconds since 1 January 1970 00:00:00 UTC.

+

+

+var day = moment(1318781876406);
+
+

+ +

String

+

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

+ +

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. +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
InputOutput
M or MMMonth
D or DDDay of month
DDD or DDDDDay of year
YY2 digit year (if greater than 70, will return 1900's, else 2000's)
YYYY4 digit year
a or AAM/PM
H, HH, h, or hh24 hour (for 12 hour time, use in conjunction with a or A)
m or mmMinutes
s or ssSeconds
+

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.

+ +

String + Formats

+

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.

+ +

Now

+

To get the current time, just call moment() with no parameters.

+

+

+var now = moment();
+
+

+

This is essentially the same as calling moment(new Date()).

+ +

Javascript Array

+

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
+
+

+ +

Manipulation

+

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);
+
+

+ +

Adding Time

+

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);
+
+

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
KeyShorthand
yearsy
monthsM
daysd
hoursh
minutesm
secondss
millisecondsms
+

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
+
+

+

Special considerations for months and years

+

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
+
+

+ +

Subtracting Time

+

This is exactly the same as moment.fn.add, only instead of adding time, it subtracts time.

+

+

+moment().subtract('days', 7);
+
+

+ +

Seconds

+

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();
+
+

+ +

Minutes

+

+

+moment().minutes(30); // set the minutes to 30
+moment().minutes(); // get the minutes value
+
+

+ +

Hours

+

+

+moment().hours(12); // set the hours to 12
+moment().hours(); // get the hours value
+
+

+ +

Day

+

+

+moment().day(5); // set the day to 5
+moment().day(); // get the day value
+
+

+ +

Month

+

+

+moment().month(5); // set the month to June
+moment().month(); // get the month value
+
+

+ +

Year

+

+

+moment().year(1984); // set the year to 1984
+moment().year(); // get the year value