var res = new Moment(checkOverflow(prepareConfig(config)));
if (res._nextDay) {
// Adding is smart enough around DST
- res.add(1, 'd');
+ res = res.add(1, 'd');
res._nextDay = undefined;
}
units = normalizeObjectUnits(units);
var prioritized = getPrioritizedUnits(units);
for (var i = 0; i < prioritized.length; i++) {
- this[prioritized[i].unit](units[prioritized[i].unit]);
+ prioritized[i].getSet.call(this, prioritized[i].value);
}
} else {
units = normalizeUnits(units);
export function startOf (units) {
units = normalizeUnits(units);
+
+ var m = this;
// the following switch intentionally omits break keywords
// to utilize falling through the cases.
switch (units) {
case 'year':
- this.month(0);
+ m = m.month(0);
/* falls through */
case 'quarter':
case 'month':
- this.date(1);
+ m = m.date(1);
/* falls through */
case 'week':
case 'isoWeek':
case 'day':
case 'date':
- this.hours(0);
+ m = m.hours(0);
/* falls through */
case 'hour':
- this.minutes(0);
+ m = m.minutes(0);
/* falls through */
case 'minute':
- this.seconds(0);
+ m = m.seconds(0);
/* falls through */
case 'second':
- this.milliseconds(0);
+ m = m.milliseconds(0);
}
// weeks are a special case
if (units === 'week') {
- this.weekday(0);
+ m = m.weekday(0);
}
if (units === 'isoWeek') {
- this.isoWeekday(1);
+ m = m.isoWeekday(1);
}
// quarters are also special
if (units === 'quarter') {
- this.month(Math.floor(this.month() / 3) * 3);
+ m = m.month(Math.floor(m.month() / 3) * 3);
}
- return this;
+ return m;
}
export function endOf (units) {
addUnitAlias('date', 'D');
-// PRIOROITY
-addUnitPriority('date', 9);
-
// PARSING
addRegexToken('D', match1to2);
// MOMENTS
export var getSetDayOfMonth = makeGetSet('Date', true);
+
+// PRIORITY
+
+addUnitPriority('date', 9, getSetDayOfMonth);
addUnitAlias('weekday', 'e');
addUnitAlias('isoWeekday', 'E');
-// PRIORITY
-addUnitPriority('day', 11);
-addUnitPriority('weekday', 11);
-addUnitPriority('isoWeekday', 11);
-
// PARSING
addRegexToken('d', match1to2);
this._weekdaysShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i');
this._weekdaysMinStrictRegex = new RegExp('^(' + minPieces.join('|') + ')', 'i');
}
+
+// PRIORITY
+
+addUnitPriority('day', 11, getSetDayOfWeek);
+addUnitPriority('weekday', 11, getSetLocaleDayOfWeek);
+addUnitPriority('isoWeekday', 11, getSetISODayOfWeek);
addUnitAlias('dayOfYear', 'DDD');
-// PRIORITY
-addUnitPriority('dayOfYear', 4);
-
// PARSING
addRegexToken('DDD', match1to3);
var dayOfYear = Math.round((this.clone().startOf('day') - this.clone().startOf('year')) / 864e5) + 1;
return input == null ? dayOfYear : this.add((input - dayOfYear), 'd');
}
+
+// PRIORITY
+
+addUnitPriority('dayOfYear', 4, getSetDayOfYear);
addUnitAlias('hour', 'h');
-// PRIORITY
-addUnitPriority('hour', 13);
-
// PARSING
function matchMeridiem (isStrict, locale) {
// a new timezone) makes sense. Adding/subtracting hours does not follow
// this rule.
export var getSetHour = makeGetSet('Hours', true);
+
+// PRIORITY
+
+addUnitPriority('hour', 13, getSetHour);
return this.millisecond() * 1000000;
});
-
// ALIASES
addUnitAlias('millisecond', 'ms');
-// PRIORITY
-
-addUnitPriority('millisecond', 16);
-
// PARSING
addRegexToken('S', match1to3, match1);
// MOMENTS
export var getSetMillisecond = makeGetSet('Milliseconds', false);
+
+// PRIORITY
+
+addUnitPriority('millisecond', 16, getSetMillisecond);
addUnitAlias('minute', 'm');
-// PRIORITY
-
-addUnitPriority('minute', 14);
-
// PARSING
addRegexToken('m', match1to2);
// MOMENTS
export var getSetMinute = makeGetSet('Minutes', false);
+
+// PRIORITY
+
+addUnitPriority('minute', 14, getSetMinute);
addUnitAlias('month', 'M');
-// PRIORITY
-
-addUnitPriority('month', 8);
-
// PARSING
addRegexToken('M', match1to2);
this._monthsStrictRegex = new RegExp('^(' + longPieces.join('|') + ')', 'i');
this._monthsShortStrictRegex = new RegExp('^(' + shortPieces.join('|') + ')', 'i');
}
+
+// PRIORITY
+
+addUnitPriority('month', 8, getSetMonth);
input = -input;
}
- this.utcOffset(input, keepLocalTime);
-
- return this;
+ return this.utcOffset(input, keepLocalTime);
} else {
return -this.utcOffset();
}
}
export function setOffsetToLocal (keepLocalTime) {
- if (this._isUTC) {
- this.utcOffset(0, keepLocalTime);
- this._isUTC = false;
+ var ret = this;
+ if (ret._isUTC) {
+ ret = ret.utcOffset(0, keepLocalTime);
+ ret._isUTC = false;
if (keepLocalTime) {
- this.subtract(getDateOffset(this), 'm');
+ ret = ret.subtract(getDateOffset(ret), 'm');
}
}
- return this;
+ return ret;
}
export function setOffsetToParsedOffset () {
if (this._tzm != null) {
- this.utcOffset(this._tzm, false, true);
+ return this.utcOffset(this._tzm, false, true);
} else if (typeof this._i === 'string') {
var tZone = offsetFromString(matchOffset, this._i);
if (tZone != null) {
- this.utcOffset(tZone);
+ return this.utcOffset(tZone);
}
else {
- this.utcOffset(0, true);
+ return this.utcOffset(0, true);
}
+ return this.utcOffset(offsetFromString(matchOffset, this._i));
}
return this;
}
var priorities = {};
-export function addUnitPriority(unit, priority) {
- priorities[unit] = priority;
+export function addUnitPriority(unit, priority, getterSetter) {
+ priorities[unit] = {priority: priority, getSet: getterSetter};
}
export function getPrioritizedUnits(unitsObj) {
var units = [];
for (var u in unitsObj) {
- units.push({unit: u, priority: priorities[u]});
+ units.push({
+ unit: u,
+ value: unitsObj[u],
+ priority: priorities[u].priority,
+ getSet: priorities[u].getSet
+ });
}
units.sort(function (a, b) {
return a.priority - b.priority;
import { addUnitPriority } from './priorities';
import { addRegexToken, match1 } from '../parse/regex';
import { addParseToken } from '../parse/token';
+import { getSetMonth } from './month';
import { MONTH } from './constants';
import toInt from '../utils/to-int';
addUnitAlias('quarter', 'Q');
-// PRIORITY
-
-addUnitPriority('quarter', 7);
-
// PARSING
addRegexToken('Q', match1);
// MOMENTS
export function getSetQuarter (input) {
- return input == null ? Math.ceil((this.month() + 1) / 3) : this.month((input - 1) * 3 + this.month() % 3);
+ return input == null ? Math.ceil((this.month() + 1) / 3) : getSetMonth.call(this, (input - 1) * 3 + this.month() % 3);
}
+
+// PRIORITY
+
+addUnitPriority('quarter', 7, getSetQuarter);
addUnitAlias('second', 's');
-// PRIORITY
-
-addUnitPriority('second', 15);
-
// PARSING
addRegexToken('s', match1to2);
// MOMENTS
export var getSetSecond = makeGetSet('Seconds', false);
+
+// PRIORITY
+
+addUnitPriority('second', 15, getSetSecond);
import { hooks } from '../utils/hooks';
import { createLocal } from '../create/local';
import { createUTCDate } from '../create/date-from-array';
+import { getSetDayOfMonth } from './day-of-month';
+import { getSetMonth } from './month';
+import { getSetYear } from './year';
// FORMATTING
addUnitAlias('weekYear', 'gg');
addUnitAlias('isoWeekYear', 'GG');
-// PRIORITY
-
-addUnitPriority('weekYear', 1);
-addUnitPriority('isoWeekYear', 1);
-
// PARSING
var dayOfYearData = dayOfYearFromWeeks(weekYear, week, weekday, dow, doy),
date = createUTCDate(dayOfYearData.year, 0, dayOfYearData.dayOfYear);
- this.year(date.getUTCFullYear());
- this.month(date.getUTCMonth());
- this.date(date.getUTCDate());
+ getSetYear.call(this, date.getUTCFullYear());
+ getSetMonth.call(this, date.getUTCMonth());
+ getSetDayOfMonth.call(this, date.getUTCDate());
return this;
}
+
+// PRIORITY
+
+addUnitPriority('weekYear', 1, getSetWeekYear);
+addUnitPriority('isoWeekYear', 1, getSetISOWeekYear);
addUnitAlias('week', 'w');
addUnitAlias('isoWeek', 'W');
-// PRIORITIES
-
-addUnitPriority('week', 5);
-addUnitPriority('isoWeek', 5);
-
// PARSING
addRegexToken('w', match1to2);
var week = weekOfYear(this, 1, 4).week;
return input == null ? week : this.add((input - week) * 7, 'd');
}
+
+// PRIORITIES
+
+addUnitPriority('week', 5, getSetWeek);
+addUnitPriority('isoWeek', 5, getSetISOWeek);
addUnitAlias('year', 'y');
-// PRIORITIES
-
-addUnitPriority('year', 1);
-
// PARSING
addRegexToken('Y', matchSigned);
export function getIsLeapYear () {
return isLeapYear(this.year());
}
+
+// PRIORITIES
+
+addUnitPriority('year', 1, getSetYear);