Created
June 13, 2016 14:11
-
-
Save geraldfullam/6141ed1e81bfd7a6d3ea28d8fe08b8e1 to your computer and use it in GitHub Desktop.
DIY Date manipulations in JavaScript: how to get days, weeks, and months from now with chainable utility methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Define new prototype methods on Date object. */ | |
// Returns Date as a String in YYYY-MM-DD format. | |
Date.prototype.toISODateString = function () { | |
return this.toISOString().substr(0,10); | |
}; | |
// Returns new Date object offset `n` days from current Date object. | |
Date.prototype.toDateFromDays = function (n) { | |
n = parseInt(n) || 0; | |
var newDate = new Date(this.getTime()); | |
newDate.setDate(this.getDate() + n); | |
return newDate; | |
}; | |
// Returns new Date object from start of week of current Date object | |
// optionally offset `n` weeks from week of current Date object. | |
Date.prototype.toStartOfWeek = function (n) { | |
var newDate = new Date(this.getTime()); | |
newDate.setDate(this.getDate() - this.getDay()); | |
return n ? newDate.toDateFromDays(n * 7) : newDate; | |
}; | |
// Returns new Date object from start of month of current Date object | |
// optionally offset `n` months from month of current Date object. | |
Date.prototype.toStartOfMonth = function (n) { | |
n = parseInt(n) || 0; | |
var newDate = new Date(this.getTime()); | |
newDate.setMonth(this.getMonth() + n, 1); | |
return newDate; | |
}; | |
/* Instantiate a Date. */ | |
var today = new Date(); | |
/* Chain all the things. */ | |
console.log( | |
'Today: ', today.toISODateString() | |
); | |
console.log( | |
'7 days ago: ', today.toDateFromDays(-7) | |
.toISODateString() | |
); | |
console.log( | |
'30 days ago: ', today.toDateFromDays(-30) | |
.toISODateString() | |
); | |
console.log( | |
'90 days from now: ', today.toDateFromDays(90) | |
.toISODateString() | |
); | |
console.log( | |
'Start of this week: ', today.toStartOfWeek() | |
.toISODateString() | |
); | |
console.log( | |
'End of this week: ', today.toStartOfWeek(1) | |
.toDateFromDays(-1) | |
.toISODateString() | |
); | |
console.log( | |
'Start of last week: ', today.toStartOfWeek(-1) | |
.toISODateString() | |
); | |
console.log( | |
'End of last week: ', today.toStartOfWeek() | |
.toDateFromDays(-1) | |
.toISODateString() | |
); | |
console.log( | |
'Start of next week: ', today.toStartOfWeek(1) | |
.toISODateString() | |
); | |
console.log( | |
'End of next week: ', today.toStartOfWeek(2) | |
.toDateFromDays(-1) | |
.toISODateString() | |
); | |
console.log( | |
'Start of this month: ', today.toStartOfMonth() | |
.toISODateString() | |
); | |
console.log( | |
'End of this month: ', today.toStartOfMonth(1) | |
.toDateFromDays(-1) | |
.toISODateString() | |
); | |
console.log( | |
'Start of last month: ', today.toStartOfMonth(-1) | |
.toISODateString() | |
); | |
console.log( | |
'End of last month: ', today.toStartOfMonth() | |
.toDateFromDays(-1) | |
.toISODateString() | |
); | |
console.log( | |
'Start of next month: ', today.toStartOfMonth(1) | |
.toISODateString() | |
); | |
console.log( | |
'End of next month: ', today.toStartOfMonth(2) | |
.toDateFromDays(-1) | |
.toISODateString() | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This micro-library was inspired by: http://stackoverflow.com/questions/37567569/javascript-how-to-get-dates-for-seven-days-ago-last-month-this-week
The following methods can be copied and used independently:
.toISODateString
,.toDateFromDays
,.toStartOfMonth
However, the method
.toStartOfWeek
is dependent on.toDateFromDays
.All methods except for
.toISODateString
— which returns a string — return chainable Date objects that can be chained with other chainable native Date methods.