We added 4 new formatting tokens. l ll lll llll
. These tokens are the the shorter counterparts to the uppercase L
tokens.
The same units exist in both formats. Choosing between an ordinal, zero filled, or plain number will depend on whether the format is uppercase or lowercase L. The same consideration should be made between long form names and abbreviated names.
moment().format('L'); // 01/14/2013
moment().format('l'); // 1/14/2013
moment().format('LL'); // January 14th 2013
moment().format('ll'); // Jan 14 2013
moment().format('LLL'); // January 14th 2013 2:08 PM
moment().format('lll'); // Jan 14 2013 2:08 PM
moment().format('LLLL'); // Monday, January 14th 2013 2:08 PM
moment().format('llll'); // Mon, Jan 14 2013 2:08 PM
The number of characters defines which types of tokens are used.
1 L/l Month numeral, day of month, year
2 LL/ll Month name, day of month, year
3 LLL/lll Month name, day of month, year, time
4 LLLL/llll Month name, day of month, day of week, year, time
If a language has been loaded into moment.js, you can now specify the language that the moment should be parsed in.
// before
moment.lang('fr');
var a = moment('2012 juillet', 'YYYY MMM');
moment.lang('en');
var b = moment('2012 July', 'YYYY MMM');
// after
var a = moment('2012 juillet', 'YYYY MMM', 'fr');
var b = moment('2012 July', 'YYYY MMM', 'en');
Due to inverted syntax between moment#add(string, number)
and moment.duration(number, string)
, we added support for reversing the arguments in add
and subtract
.
The following syntaxes are now supported.
moment().add({seconds:1});
moment().add('seconds', 1);
moment().add('seconds', '1');
moment().add(1, 'seconds');
Due to implementation details, the following syntax is not supported. The first argument should be typecast to a number in this instance.
moment().add('1', 'seconds');
These set the moment to the beginning or end of the week. The first day of the week is Sunday.
Because the most common use case for month and year diffs is birthday calculation and age gating, the difference calculation has been optimized for comparing two moments with the same day of the month.
Jan 15 to Feb 15 is exactly 1 month. Feb 28 to Mar 28 is exactly 1 month. Feb 28 2011 to Feb 28 2012 is exactly 1 year.
The values for different moments without the same day of the month are also as expected.
Jan 31 to Feb 28 is slightly less than 1 month. Jan 31 to Mar 1 is slightly more than 1 month.
The implimentation details can be seen here: moment/moment#571 (comment)
moment([2012, 0, 1]).diff([2012, 0, 1], 'months'); // 0
moment([2012, 0, 1]).diff([2012, 0, 31], 'months'); // 0
moment([2012, 0, 1]).diff([2012, 1, 1], 'months'); // 1
moment([2012, 0, 1]).diff([2012, 0, 31], 'months', true); // .9677
moment([2012, 0, 1, 0]).diff([2012, 0, 1, 23], 'days'); // 0
moment([2012, 0, 1, 0]).diff([2012, 0, 1, 23], 'days', true); // .9583
Calling moment().toString()
is now equivalent to moment().format("ddd MMM DD YYYY HH:mm:ss [GMT]ZZ")
in order to normalize behavior across environments.
These three new methods all accept a units parameter of any string that can be used in startOf
or endOf
.
They return a boolean as you would expect from their method names.
moment().isBefore(otherMoment);
moment().isAfter(otherMoment, 'day');
moment().isSame(otherMoment, 'month');
We added a getter/setter for week
and for isoWeek
. The week
getter/setter uses the moment's language config to determine which day starts the week, and which week starts the year. The isoWeek
getter/setter uses the ISO week numbering.
The W WW Wo
tokens have also been added, which display the iso week number. The w ww wo
tokens now use the week
getter/setters, and thus use the language settings for which week numbering system to use.
The dayOfYear
getter/setter method was also added in support of the weeks addition.
Now, when exporting a moment with JSON.stringify
, the moment will be represented as a string with the format 'YYYY-MM-DD[T]HH:mm:ss.SSS[Z]'.
This is in line with the implementation of Date#toJSON
in environments that support it.
Because that's what cool kids do.
In order to better support internationalization, the ordinal
method now requires that you return the number and the ordinal instead of simply the ordinal. This allows languages to eliminate the number altogether if needed.
Additionally, if you set the ordinal property to a string, the first %d
in the string will be replaced with the number to be ordinalized.
Before, '70 = 2070
and '71 = 1971
. Now, '68 = 2068
and '69 = 1969
.
Now, to get the lang data, use moment.langData()
for the global language, or better yet, moment#lang()
for the instance language.
Now, a preexisting date passed in to moment()
does not get referenced. Instead, a new Date
is created with the value of the date passed in.
var d = new Date();
var m = moment(d);
// m._d !== d
- Arabic (ar)
- Moroccan Arabic (ar-ma)
- Czech (cs)
- Esperanto (eo)
- Hebrew (he)
- Bahasa Indonesia (id)
- Latvian (lv)
- Nepalese (ne)
- Slovenian (sl)
- Thai (th)
- Morocco Central Atlas Tamaziɣt in Latin (tzm-la)
- Morocco Central Atlas Tamaziɣt (tzm)
- Ukrainian (uk)
oha, i just see that your gist is marked as secret but is still public readable.
good to know ...