Skip to content

Commit

Permalink
Moved function to trim a date to the month only.
Browse files Browse the repository at this point in the history
  • Loading branch information
FlukeFan committed Jan 9, 2012
1 parent 1d98d25 commit 09431a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
13 changes: 2 additions & 11 deletions Source/OfflineExample/Scripts/Offline/StorageService.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,15 @@ StorageService.prototype.execute = function (cmd, callback) {
});
}

// TODO: add this to the Date prototype for cleaner code
function toYearMonth(date) {
var yearMonth = new Date();
yearMonth.setTime(0);
yearMonth.setFullYear(date.getFullYear());
yearMonth.setMonth(date.getMonth());
return yearMonth;
}

StorageService.prototype.getAllByMonth = function () {
var rawList = this.getRawList();
var result = [];

rawList.forEach(function (rawObj) {
var app = new Appointment(rawObj);

if ((result.length == 0) || (result[result.length - 1].getMonth().valueOf() != toYearMonth(app.getVisitDate()).valueOf()))
result.push(new AppointmentMonth().setMonth(toYearMonth(app.getVisitDate())));
if ((result.length == 0) || (result[result.length - 1].getMonth().valueOf() != app.getVisitDate().getTrimToMonth().valueOf()))
result.push(new AppointmentMonth().setMonth(app.getVisitDate().getTrimToMonth()));

var last = result[result.length - 1];
last.setCount(last.getCount() + 1);
Expand Down
4 changes: 4 additions & 0 deletions Source/OfflineExample/Scripts/Util/DateUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@
return shortMonth[this.getMonth()];
}

Date.prototype.getTrimToMonth = function () {
return new Date(this.getFullYear(), this.getMonth(), 1);
}

})();
8 changes: 8 additions & 0 deletions Source/OfflineExample/Scripts/Util/spec/DateUtil.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ describe("DateUtil tests", function () {
expect(shortName).toBe("Feb");
});

it("should return the 1st of the month for a given date", function () {
var dte = new Date("08-Feb-2003 04:05:67");

var monthOnly = dte.getTrimToMonth();

expect(monthOnly.valueOf()).toBe(new Date("01 Feb 2003").valueOf());
});

});

0 comments on commit 09431a5

Please sign in to comment.