From: Tim Wood Date: Thu, 1 Dec 2011 18:38:11 +0000 (-0800) Subject: Adding LDML difference disclaimer #61 X-Git-Tag: 1.2.0~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a12750edc85b2b894a6ce4e1b00cdd5294b5a15;p=thirdparty%2Fmoment.git Adding LDML difference disclaimer #61 --- diff --git a/site/docs/index.html b/site/docs/index.html index 618c74e34..e97758dd8 100644 --- a/site/docs/index.html +++ b/site/docs/index.html @@ -1,4 +1,4 @@ -Moment.js Documentation

Moment.js Documentation

A lightweight javascript date library for parsing, manipulating, and formatting dates.

Where to get it

Github

Production Version 1.1.22.8kb minified & gzippedDevelopment Version 1.1.220.1kb full source + comments

You can also clone the project with Git by running:

git clone git://github.com/timrwood/moment

npm

npm install moment

Where to use it

Moment was designed to work in both the browser and in NodeJS. All code will work in both environments. All unit tests are run in both environments.

In NodeJS

var moment = require('moment');
+Moment.js Documentation

Moment.js Documentation

A lightweight javascript date library for parsing, manipulating, and formatting dates.

Where to get it

Github

Production Version 1.1.22.8kb minified & gzippedDevelopment Version 1.1.220.1kb full source + comments

You can also clone the project with Git by running:

git clone git://github.com/timrwood/moment

npm

npm install moment

Where to use it

Moment was designed to work in both the browser and in NodeJS. All code will work in both environments. All unit tests are run in both environments.

In NodeJS

var moment = require('moment');
 moment().add('hours', 1).fromNow(); // "1 hour ago"
 

In the browser

<script src="moment.min.js"></script>
 moment().add('hours', 1).fromNow(); // "1 hour ago"
@@ -12,15 +12,20 @@ var dayWrapper = moment(day);
 

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

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
Z or ZZTimezone offset as +0700 or ++07:30

Unless you specify a timezone offset, parsing a string will create a date in the current timezone.

A workaround to parse a string in UTC is to append "+0000" to the end of your input string, and add +"ZZ"to the end of your format string. +

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

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, where there is a difference in big, medium, or little endian date formats.

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

Javascript Array

An array mirroring the parameters passed into new Date()

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

Construction with an array will create a date in the current timezone. To create a date from an array at UTC, you can use the following.

var array = [2010, 1, 14, 15, 25, 50, 125];var day = moment(Date.UTC.apply({}, array));
 

Manipulation

Once you have a Moment.js wrapper object, you may want to manipulate it in some way. There are a number of moment.fnmethods 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
weeksw
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 
@@ -40,7 +45,7 @@ moment().seconds() === new Date().getSeconds();
 

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"
-
TokenOutput
Month
M1 2 ... 11 12
Mo1st 2nd ... 11th 12th
MM01 02 ... 11 12
MMMJan Feb ... Nov Dec
MMMMJanuary February ... November December
Day of Month
D1 2 ... 30 30
Do1st 2nd ... 30th 31st
DD01 02 ... 30 31
Day of Year
DDD1 2 ... 364 365
DDDo1st 2nd ... 364th 365th
DDDD001 002 ... 364 365
Day of Week
d0 1 ... 5 6
do0th 1st ... 5th 6th
dddSun Mon ... Fri Sat
ddddSunday Monday ... Friday Saturday
Week of Year
w1 2 ... 52 53
wo1st 2nd ... 52nd 53rd
ww01 02 ... 52 53
Year
YY70 71 ... 29 30
YYYY1970 1971 ... 2029 2030
AM/PM
AAM PM
aam pm
Hour
H0 1 ... 22 23
HH00 01 ... 22 23
h1 2 ... 11 12
hh01 02 ... 11 12
Minute
m0 1 ... 58 59
mm00 01 ... 58 59
Second
s0 1 ... 58 59
ss00 01 ... 58 59
Timezone
zEST CST ... MST PST
Localized date format
L07/10/1986
LLJuly 10 1986
LLLJuly 10 1986 8:30 PM
LLLLSaturday, July 10 1986 8:30 PM

Time from another moment

Another common way of displaying time, sometimes called timeago, is handled by moment.fn.from. +

TokenOutput
Month
M1 2 ... 11 12
Mo1st 2nd ... 11th 12th
MM01 02 ... 11 12
MMMJan Feb ... Nov Dec
MMMMJanuary February ... November December
Day of Month
D1 2 ... 30 30
Do1st 2nd ... 30th 31st
DD01 02 ... 30 31
Day of Year
DDD1 2 ... 364 365
DDDo1st 2nd ... 364th 365th
DDDD001 002 ... 364 365
Day of Week
d0 1 ... 5 6
do0th 1st ... 5th 6th
dddSun Mon ... Fri Sat
ddddSunday Monday ... Friday Saturday
Week of Year
w1 2 ... 52 53
wo1st 2nd ... 52nd 53rd
ww01 02 ... 52 53
Year
YY70 71 ... 29 30
YYYY1970 1971 ... 2029 2030
AM/PM
AAM PM
aam pm
Hour
H0 1 ... 22 23
HH00 01 ... 22 23
h1 2 ... 11 12
hh01 02 ... 11 12
Minute
m0 1 ... 58 59
mm00 01 ... 58 59
Second
s0 1 ... 58 59
ss00 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
L07/10/1986
LLJuly 10 1986
LLLJuly 10 1986 8:30 PM
LLLLSaturday, 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.

Time from another moment

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"
@@ -197,7 +202,7 @@ moment(1316116057189).fromNow(); // il y a une heure
         (b === 2) ? 'nd' : 
         (b === 3) ? 'rd' : 'th';
 };
-

For more information on ordinal numbers, see wikipedia