Skip to content

Commit

Permalink
Merge pull request #3177 from nvdh:bugfix-2704
Browse files Browse the repository at this point in the history
Bug Fix #2704 - isoWeekday(String) inconsistent with isoWeekday(Number)
  • Loading branch information
ichernev committed Jun 14, 2016
2 parents e3439fa + f9cd922 commit 07358ff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/lib/units/day-of-week.js
Expand Up @@ -88,6 +88,13 @@ function parseWeekday(input, locale) {
return null;
}

function parseIsoWeekday(input, locale) {
if (typeof input === 'string') {
return locale.weekdaysParse(input) % 7 || 7;
}
return isNaN(input) ? null : input;
}

// LOCALES

export var defaultLocaleWeekdays = 'Sunday_Monday_Tuesday_Wednesday_Thursday_Friday_Saturday'.split('_');
Expand Down Expand Up @@ -237,10 +244,17 @@ export function getSetISODayOfWeek (input) {
if (!this.isValid()) {
return input != null ? this : NaN;
}

// behaves the same as moment#day except
// as a getter, returns 7 instead of 0 (1-7 range instead of 0-6)
// as a setter, sunday should belong to the previous week.
return input == null ? this.day() || 7 : this.day(this.day() % 7 ? input : input - 7);

if (input != null) {
var weekday = parseIsoWeekday(input, this.localeData());
return this.day(this.day() % 7 ? weekday : weekday - 7);
} else {
return this.day() || 7;
}
}

export var defaultWeekdaysRegex = matchWord;
Expand Down
19 changes: 19 additions & 0 deletions src/test/moment/weekday.js
Expand Up @@ -53,6 +53,25 @@ test('iso weekday setter', function (assert) {
assert.equal(moment(a).isoWeekday(14).date(), 23, 'set from sun to next sun');
});

test('iso weekday setter with day name', function (assert) {
moment.locale('en');

var a = moment([2011, 0, 10]);
assert.equal(moment(a).isoWeekday('Monday').date(), 10, 'set from mon to mon');
assert.equal(moment(a).isoWeekday('Thursday').date(), 13, 'set from mon to thu');
assert.equal(moment(a).isoWeekday('Sunday').date(), 16, 'set from mon to sun');

a = moment([2011, 0, 13]);
assert.equal(moment(a).isoWeekday('Monday').date(), 10, 'set from thu to mon');
assert.equal(moment(a).isoWeekday('Thursday').date(), 13, 'set from thu to thu');
assert.equal(moment(a).isoWeekday('Sunday').date(), 16, 'set from thu to sun');

a = moment([2011, 0, 16]);
assert.equal(moment(a).isoWeekday('Monday').date(), 10, 'set from sun to mon');
assert.equal(moment(a).isoWeekday('Thursday').date(), 13, 'set from sun to thu');
assert.equal(moment(a).isoWeekday('Sunday').date(), 16, 'set from sun to sun');
});

test('weekday first day of week Sunday (dow 0)', function (assert) {
moment.locale('dow: 0,doy: 6', {week: {dow: 0, doy: 6}});
assert.equal(moment([1985, 1, 3]).weekday(), 0, 'Feb 3 1985 is Sunday -- 0th day');
Expand Down

0 comments on commit 07358ff

Please sign in to comment.