function dateAddRemove(date, _input, adding, val) {
var isString = (typeof _input === 'string'),
input = isString ? {} : _input,
- ms, M, currentDate;
+ ms, d, M, currentDate;
if (isString && val) {
input[_input] = val;
}
ms = (input.ms || input.milliseconds || 0) +
(input.s || input.seconds || 0) * 1e3 + // 1000
(input.m || input.minutes || 0) * 6e4 + // 1000 * 60
- (input.h || input.hours || 0) * 36e5 + // 1000 * 60 * 60
- (input.d || input.days || 0) * 864e5 + // 1000 * 60 * 60 * 24
- (input.w || input.weeks || 0) * 6048e5; // 1000 * 60 * 60 * 24 * 7
+ (input.h || input.hours || 0) * 36e5; // 1000 * 60 * 60
+ d = (input.d || input.days || 0) +
+ (input.w || input.weeks || 0) * 7;
M = (input.M || input.months || 0) +
(input.y || input.years || 0) * 12;
if (ms) {
date.setTime(+date + ms * adding);
}
+ if (d) {
+ date.setDate(date.getDate() + d * adding);
+ }
if (M) {
currentDate = date.getDate();
date.setDate(1);
p Example:
pre moment([2010, 0, 31]); // January 31 \n
| moment([2010, 0, 31]).add('months', 1); // February 28
+ p There are also special considerations to keep in mind when adding time that crosses over Daylight Savings Time.
+ | If you are adding years, months, weeks, or days, the original hour will always match the added hour.
+ pre var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US\n
+ | m.hours(); // 5
+ | m.add('days', 1).hours(); // 5
+ p If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour.
+ pre var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US\n
+ | m.hours(); // 5
+ | m.add('hours', 24).hours(); // 6
a(name="/manipulation/subtract")
p Once you load a language, it becomes the active language. To change active languages, simply call
code moment.lang
| with the key of a loaded language.
- pre moment.lang('fr');
+ pre moment.lang('fr');\n
| moment(1316116057189).fromNow() // il y a une heure
| moment.lang('en');
| moment(1316116057189).fromNow() // an hour ago
code moment.lang
| will load it.
pre var moment = require('moment');\n
- | moment.lang('fr');\n
+ | moment.lang('fr');
| moment(1316116057189).fromNow(); // il y a une heure
p Right now, there is only support for English, French, Italian, and Portuguese.
| If you want your language supported, create a pull request or send me an email with the
equal(a.add('y', 1).year(), 2012, 'Add year');
});
+test("adding across DST", 3, function(){
+ var a = moment(new Date(2011, 2, 12, 5, 0, 0));
+ var b = moment(new Date(2011, 2, 12, 5, 0, 0));
+ var c = moment(new Date(2011, 2, 12, 5, 0, 0));
+ a.add('days', 1);
+ b.add('hours', 24);
+ c.add('months', 1);
+ equal(a.hours(), 5, 'adding days over DST difference should result in the same hour');
+ equal(b.hours(), 6, 'adding hours over DST difference should result in a different hour');
+ equal(c.hours(), 5, 'adding months over DST difference should result in the same hour');
+});
+
module("diff");