}
},
+ deprecations = {},
+
lists = ['months', 'monthsShort', 'weekdays', 'weekdaysShort', 'weekdaysMin'];
// Pick the first defined of two or three arguments. dfl comes from
};
}
+ function printMsg(msg) {
+ if (moment.suppressDeprecationWarnings === false &&
+ typeof console !== 'undefined' && console.warn) {
+ console.warn("Deprecation warning: " + msg);
+ }
+ }
+
function deprecate(msg, fn) {
var firstTime = true;
- function printMsg() {
- if (moment.suppressDeprecationWarnings === false &&
- typeof console !== 'undefined' && console.warn) {
- console.warn("Deprecation warning: " + msg);
- }
- }
return extend(function () {
if (firstTime) {
- printMsg();
+ printMsg(msg);
firstTime = false;
}
return fn.apply(this, arguments);
}, fn);
}
+ function deprecateSimple(name, msg) {
+ if (!deprecations[name]) {
+ printMsg(msg);
+ deprecations[name] = true;
+ }
+ }
+
function padToken(func, count) {
return function (a) {
return leftZeroFill(func.call(this, a), count);
return res;
}
- // helper function for _.addTime and _.subtractTime
+ //todo: remove 'name' arg after deprecation is removed
+ function createAdder(direction, name) {
+ return function (val, period) {
+ var dur, tmp;
+ //invert the arguments, but complain about it
+ if (period !== null && !isNaN(+period)) {
+ deprecateSimple(name, "moment()." + name + "(period, number) is deprecated. Please use moment()." + name + "(number, period).");
+ tmp = val; val = period; period = tmp;
+ }
+
+ val = typeof val === 'string' ? +val : val;
+ dur = moment.duration(val, period);
+ addOrSubtractDurationFromMoment(this, dur, direction);
+ return this;
+ };
+ }
+
function addOrSubtractDurationFromMoment(mom, duration, isAdding, updateOffset) {
var milliseconds = duration._milliseconds,
days = duration._days,
daysToDayOfWeek += 7;
}
- adjustedMoment = moment(mom).add('d', daysToDayOfWeek);
+ adjustedMoment = moment(mom).add(daysToDayOfWeek, 'd');
return {
week: Math.ceil(adjustedMoment.dayOfYear() / 7),
year: adjustedMoment.year()
return this.lang().postformat(output);
},
- add : function (input, val) {
- var dur;
- // switch args to support add('s', 1) and add(1, 's')
- if (typeof input === 'string' && typeof val === 'string') {
- dur = moment.duration(isNaN(+val) ? +input : +val, isNaN(+val) ? val : input);
- } else if (typeof input === 'string') {
- dur = moment.duration(+val, input);
- } else {
- dur = moment.duration(input, val);
- }
- addOrSubtractDurationFromMoment(this, dur, 1);
- return this;
- },
+ add : createAdder(1, 'add'),
- subtract : function (input, val) {
- var dur;
- // switch args to support subtract('s', 1) and subtract(1, 's')
- if (typeof input === 'string' && typeof val === 'string') {
- dur = moment.duration(isNaN(+val) ? +input : +val, isNaN(+val) ? val : input);
- } else if (typeof input === 'string') {
- dur = moment.duration(+val, input);
- } else {
- dur = moment.duration(input, val);
- }
- addOrSubtractDurationFromMoment(this, dur, -1);
- return this;
- },
+ subtract : createAdder(-1, 'subtract'),
diff : function (input, units, asFloat) {
var that = makeAs(input, this),
var day = this._isUTC ? this._d.getUTCDay() : this._d.getDay();
if (input != null) {
input = parseWeekday(input, this.lang());
- return this.add(input - day, 'days');
+ return this.add(input - day, 'd');
} else {
return day;
}
month : makeAccessor('Month', true),
- startOf: function (units) {
+ startOf : function (units) {
units = normalizeUnits(units);
// the following switch intentionally omits break keywords
// to utilize falling through the cases.
endOf: function (units) {
units = normalizeUnits(units);
- return this.startOf(units).add((units === 'isoWeek' ? 'week' : units), 1).subtract('ms', 1);
+ return this.startOf(units).add(1, (units === 'isoWeek' ? 'week' : units)).subtract(1, 'ms');
},
isAfter: function (input, units) {
dayOfYear : function (input) {
var dayOfYear = round((moment(this).startOf('day') - moment(this).startOf('year')) / 864e5) + 1;
- return input == null ? dayOfYear : this.add("d", (input - dayOfYear));
+ return input == null ? dayOfYear : this.add((input - dayOfYear), 'd');
},
quarter : function (input) {
weekYear : function (input) {
var year = weekOfYear(this, this.lang()._week.dow, this.lang()._week.doy).year;
- return input == null ? year : this.add("y", (input - year));
+ return input == null ? year : this.add((input - year), 'y');
},
isoWeekYear : function (input) {
var year = weekOfYear(this, 1, 4).year;
- return input == null ? year : this.add("y", (input - year));
+ return input == null ? year : this.add((input - year), 'y');
},
week : function (input) {
var week = this.lang().week(this);
- return input == null ? week : this.add("d", (input - week) * 7);
+ return input == null ? week : this.add((input - week) * 7, 'd');
},
isoWeek : function (input) {
var week = weekOfYear(this, 1, 4).week;
- return input == null ? week : this.add("d", (input - week) * 7);
+ return input == null ? week : this.add((input - week) * 7, 'd');
},
weekday : function (input) {
var weekday = (this.day() + 7 - this.lang()._week.dow) % 7;
- return input == null ? weekday : this.add("d", input - weekday);
+ return input == null ? weekday : this.add(input - weekday, 'd');
},
isoWeekday : function (input) {
}
for (i = 0; i < DDDo.length; i++) {
DDDoDt = moment([2010]);
- test.equal(DDDoDt.add('days', DDDo[i][0]).format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
+ test.equal(DDDoDt.add(DDDo[i][0], 'days').format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
}
test.done();
},
}
for (i = 0; i < DDDo.length; i++) {
DDDoDt = moment([2010]);
- test.equal(DDDoDt.add('days', DDDo[i][0]).format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
+ test.equal(DDDoDt.add(DDDo[i][0], 'days').format('DDDo'), DDDo[i][1], DDDo[i][0] + ' ---> ' + DDDo[i][1]);
}
test.done();
},
done();
},
- "add short" : function (test) {
+ "add short reverse args" : function (test) {
test.expect(16);
var a = moment(), b, c, d;
test.done();
},
- "add long" : function (test) {
+ "add long reverse args" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "add long singular" : function (test) {
+ "add long singular reverse args" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "add string long" : function (test) {
+ "add string long reverse args" : function (test) {
test.expect(10);
var a = moment(), b;
test.done();
},
- "add string long singular" : function (test) {
+ "add string long singular reverse args" : function (test) {
test.expect(10);
var a = moment(), b;
test.done();
},
- "add string short" : function (test) {
+ "add string short reverse args" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "add string long reverse args" : function (test) {
+ "add string long" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "add string long singular reverse args" : function (test) {
+ "add string long singular" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "add string short reverse args" : function (test) {
+ "add string short" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "add strings string short reverse args" : function (test) {
+ "add strings string short" : function (test) {
test.expect(9);
var a = moment();
test.done();
},
- "subtract strings string short reverse args" : function (test) {
+ "subtract strings string short" : function (test) {
test.expect(9);
var a = moment();
c = moment(new Date(2011, 2, 12, 5, 0, 0)),
d = moment(new Date(2011, 2, 12, 5, 0, 0)),
e = moment(new Date(2011, 2, 12, 5, 0, 0));
- a.add('days', 1);
- b.add('hours', 24);
- c.add('months', 1);
- e.add('quarter', 1);
+ a.add(1, 'days');
+ b.add(24, 'hours');
+ c.add(1, 'months');
+ e.add(1, 'quarter');
test.equal(a.hours(), 5, 'adding days over DST difference should result in the same hour');
if (b.isDST() && !d.isDST()) {
test.equal(b.hours(), 6, 'adding hours over DST difference should result in a different hour');
moment.lang('es');
test.equal(moment().lang(lang).calendar(), "sameDay -LT-", "Should use instance lang in LT formatting");
- test.equal(moment().add('days', 1).lang(lang).calendar(), "nextDay -L-", "Should use instance lang in L formatting");
- test.equal(moment().add('days', -1).lang(lang).calendar(), "lastDay -LLL-", "Should use instance lang in LL formatting");
- test.equal(moment().add('days', 4).lang(lang).calendar(), "nextWeek -LL-", "Should use instance lang in LLL formatting");
- test.equal(moment().add('days', -4).lang(lang).calendar(), "lastWeek -LLLL-", "Should use instance lang in LLLL formatting");
+ test.equal(moment().add(1, 'days').lang(lang).calendar(), "nextDay -L-", "Should use instance lang in L formatting");
+ test.equal(moment().add(-1, 'days').lang(lang).calendar(), "lastDay -LLL-", "Should use instance lang in LL formatting");
+ test.equal(moment().add(4, 'days').lang(lang).calendar(), "nextWeek -LL-", "Should use instance lang in LLL formatting");
+ test.equal(moment().add(-4, 'days').lang(lang).calendar(), "lastWeek -LLLL-", "Should use instance lang in LLLL formatting");
test.done();
},
var start = moment([2007, 1, 28]);
test.equal(start.from(moment([2007, 1, 28]).add({s: 90}), true), "@ minutes", "postformat should work on moment.fn.from");
- test.equal(moment().add('d', 6).fromNow(true), "^ days", "postformat should work on moment.fn.fromNow");
+ test.equal(moment().add(6, 'd').fromNow(true), "^ days", "postformat should work on moment.fn.fromNow");
test.equal(moment.duration(10, "h").humanize(), "!) hours", "postformat should work on moment.duration.fn.humanize");
test.done();
var a = moment();
// Seconds to minutes threshold
- a.subtract('seconds', 44);
+ a.subtract(44, 'seconds');
test.equal(a.fromNow(), "a few seconds ago", "Below default seconds to minutes threshold");
- a.subtract('seconds', 1);
+ a.subtract(1, 'seconds');
test.equal(a.fromNow(), "a minute ago", "Above default seconds to minutes threshold");
// Minutes to hours threshold
a = moment();
- a.subtract('minutes', 44);
+ a.subtract(44, 'minutes');
test.equal(a.fromNow(), "44 minutes ago", "Below default minute to hour threshold");
- a.subtract('minutes', 1);
+ a.subtract(1, 'minutes');
test.equal(a.fromNow(), "an hour ago", "Above default minute to hour threshold");
// Hours to days threshold
a = moment();
- a.subtract('hours', 21);
+ a.subtract(21, 'hours');
test.equal(a.fromNow(), "21 hours ago", "Below default hours to day threshold");
- a.subtract('hours', 1);
+ a.subtract(1, 'hours');
test.equal(a.fromNow(), "a day ago", "Above default hours to day threshold");
// Days to month threshold
a = moment();
- a.subtract('days', 25);
+ a.subtract(25, 'days');
test.equal(a.fromNow(), "25 days ago", "Below default days to month (singular) threshold");
- a.subtract('days', 1);
+ a.subtract(1, 'days');
test.equal(a.fromNow(), "a month ago", "Above default days to month (singular) threshold");
// months to year threshold
moment.relativeTimeThreshold('s', 55);
var a = moment();
- a.subtract('seconds', 54);
+ a.subtract(54, 'seconds');
test.equal(a.fromNow(), "a few seconds ago", "Below custom seconds to minutes threshold");
- a.subtract('seconds', 1);
+ a.subtract(1, 'seconds');
test.equal(a.fromNow(), "a minute ago", "Above custom seconds to minutes threshold");
moment.relativeTimeThreshold('s', 45);
// Minutes to hours threshold
moment.relativeTimeThreshold('m', 55);
a = moment();
- a.subtract('minutes', 54);
+ a.subtract(54, 'minutes');
test.equal(a.fromNow(), "54 minutes ago", "Below custom minutes to hours threshold");
- a.subtract('minutes', 1);
+ a.subtract(1, 'minutes');
test.equal(a.fromNow(), "an hour ago", "Above custom minutes to hours threshold");
moment.relativeTimeThreshold('m', 45);
// Hours to days threshold
moment.relativeTimeThreshold('h', 24);
a = moment();
- a.subtract('hours', 23);
+ a.subtract(23, 'hours');
test.equal(a.fromNow(), "23 hours ago", "Below custom hours to days threshold");
- a.subtract('hours', 1);
+ a.subtract(1, 'hours');
test.equal(a.fromNow(), "a day ago", "Above custom hours to days threshold");
moment.relativeTimeThreshold('h', 22);
// Days to month threshold
moment.relativeTimeThreshold('d', 28);
a = moment();
- a.subtract('days', 27);
+ a.subtract(27, 'days');
test.equal(a.fromNow(), "27 days ago", "Below custom days to month (singular) threshold");
- a.subtract('days', 1);
+ a.subtract(1, 'days');
test.equal(a.fromNow(), "a month ago", "Above custom days to month (singular) threshold");
moment.relativeTimeThreshold('d', 26);
// months to years threshold
moment.relativeTimeThreshold('M', 9);
a = moment();
- a.subtract('months', 8);
+ a.subtract(8, 'months');
test.equal(a.fromNow(), "8 months ago", "Below custom days to years threshold");
- a.subtract('months', 1);
+ a.subtract(1, 'months');
test.equal(a.fromNow(), "a year ago", "Above custom days to years threshold");
moment.relativeTimeThreshold('M', 11);
test.done();
test.equal(m.format("HH:mm"), "00:00", "should start 12AM at +0000 timezone");
m.__doChange = true;
- m.add('h', 1);
+ m.add(1, 'h');
test.equal(m.format("ZZ"), "-0200", "should be at -0200");
test.equal(m.format("HH:mm"), "23:00", "1AM at +0000 should be 11PM at -0200 timezone");
- m.subtract('h', 1);
+ m.subtract(1, 'h');
test.equal(m.format("ZZ"), "-0100", "should be at -0100");
test.equal(m.format("HH:mm"), "23:00", "12AM at +0000 should be 11PM at -0100 timezone");
zoneB = moment(zoneA).zone(720),
zoneC = moment(zoneA).zone(360),
zoneD = moment(zoneA).zone(-690),
- other = moment(zoneA).add('m', 35);
+ other = moment(zoneA).add(35, 'm');
test.equal(zoneA.from(other), zoneB.from(other), "moment#from should be the same in all zones");
test.equal(zoneA.from(other), zoneC.from(other), "moment#from should be the same in all zones");
zoneB = moment(zoneA).zone(720),
zoneC = moment(zoneA).zone(360),
zoneD = moment(zoneA).zone(-690),
- other = moment(zoneA).add('m', 35);
+ other = moment(zoneA).add(35, 'm');
test.equal(zoneA.diff(other), zoneB.diff(other), "moment#diff should be the same in all zones");
test.equal(zoneA.diff(other), zoneC.diff(other), "moment#diff should be the same in all zones");
test.ok(zoneA.isSame(zoneB, 'hour'), "two moments with different offsets should be the same hour");
test.ok(zoneA.isSame(zoneC, 'hour'), "two moments with different offsets should be the same hour");
- zoneA.add('hour', 1);
+ zoneA.add(1, 'hour');
test.ok(zoneA.isAfter(zoneB), "isAfter should work with two moments with different offsets");
test.ok(zoneA.isAfter(zoneC), "isAfter should work with two moments with different offsets");
test.ok(zoneA.isAfter(zoneB, 'hour'), "isAfter:hour should work with two moments with different offsets");
test.ok(zoneA.isAfter(zoneC, 'hour'), "isAfter:hour should work with two moments with different offsets");
- zoneA.subtract('hour', 2);
+ zoneA.subtract(2, 'hour');
test.ok(zoneA.isBefore(zoneB), "isBefore should work with two moments with different offsets");
test.ok(zoneA.isBefore(zoneC), "isBefore should work with two moments with different offsets");
test.equal(m.hour(), 3, "should start at 00:00");
- m.add('hour', 24);
+ m.add(24, 'hour');
test.equal(m.hour(), 4, "adding 24 hours should disregard dst");
- m.subtract('hour', 24);
+ m.subtract(24, 'hour');
test.equal(m.hour(), 3, "subtracting 24 hours should disregard dst");
- m.add('day', 1);
+ m.add(1, 'day');
test.equal(m.hour(), 3, "adding 1 day should have the same hour");
- m.subtract('day', 1);
+ m.subtract(1, 'day');
test.equal(m.hour(), 3, "subtracting 1 day should have the same hour");
- m.add('month', 1);
+ m.add(1, 'month');
test.equal(m.hour(), 3, "adding 1 month should have the same hour");
- m.subtract('month', 1);
+ m.subtract(1, 'month');
test.equal(m.hour(), 3, "subtracting 1 month should have the same hour");