Skip to content

Commit

Permalink
Adding moment.fn.day as a setter.
Browse files Browse the repository at this point in the history
  • Loading branch information
timrwood committed Dec 22, 2011
1 parent f91fcfa commit 0a29ed6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
11 changes: 6 additions & 5 deletions moment.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@

isDST : function () {
return this.zone() !== moment([this.year()]).zone();
},

day : function (input) {
var day = this._d.getDay();
return input == null ? day :
this.add({ d : input - day });
}
};

Expand All @@ -584,11 +590,6 @@
// add shortcut for year (uses different syntax than the getter/setter 'year' == 'FullYear')
makeShortcut('year', 'FullYear');

// add shortcut for day (no setter)
moment.fn.day = function () {
return this._d.getDay();
};

// add shortcut for timezone offset (no setter)
moment.fn.zone = function () {
return this._d.getTimezoneOffset();
Expand Down
29 changes: 29 additions & 0 deletions sitesrc/js/unit-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,35 @@ test("chaining setters", 7, function() {
equal(a.seconds(), 8, 'second');
});

test("day setter", 18, function() {
var a = moment([2011, 0, 15]);
equal(moment(a).day(0).date(), 9, 'set from saturday to sunday');
equal(moment(a).day(6).date(), 15, 'set from saturday to saturday');
equal(moment(a).day(3).date(), 12, 'set from saturday to wednesday');

a = moment([2011, 0, 9]);
equal(moment(a).day(0).date(), 9, 'set from sunday to sunday');
equal(moment(a).day(6).date(), 15, 'set from sunday to saturday');
equal(moment(a).day(3).date(), 12, 'set from sunday to wednesday');

a = moment([2011, 0, 12]);
equal(moment(a).day(0).date(), 9, 'set from wednesday to sunday');
equal(moment(a).day(6).date(), 15, 'set from wednesday to saturday');
equal(moment(a).day(3).date(), 12, 'set from wednesday to wednesday');

equal(moment(a).day(-7).date(), 2, 'set from wednesday to last sunday');
equal(moment(a).day(-1).date(), 8, 'set from wednesday to last saturday');
equal(moment(a).day(-4).date(), 5, 'set from wednesday to last wednesday');

equal(moment(a).day(7).date(), 16, 'set from wednesday to next sunday');
equal(moment(a).day(13).date(), 22, 'set from wednesday to next saturday');
equal(moment(a).day(10).date(), 19, 'set from wednesday to next wednesday');

equal(moment(a).day(14).date(), 23, 'set from wednesday to second next sunday');
equal(moment(a).day(20).date(), 29, 'set from wednesday to second next saturday');
equal(moment(a).day(17).date(), 26, 'set from wednesday to second next wednesday');
});


module("format");

Expand Down

0 comments on commit 0a29ed6

Please sign in to comment.