Skip to content

Instantly share code, notes, and snippets.

@sp3c73r2038
Created January 28, 2011 03:22
Show Gist options
  • Save sp3c73r2038/799775 to your computer and use it in GitHub Desktop.
Save sp3c73r2038/799775 to your computer and use it in GitHub Desktop.
function getFirstDayOfWeek(date) {
if (!date) {
date = new Date();
}
var firstDay = new Date(date - (date.getDay() - 1) * 86400000);
return firstDay;
}
function getLastDayOfWeek(date) {
if (!date) {
date = new Date();
}
var firstDay = new Date(date - (date.getDay() - 1) * 86400000);
var lastDay = new Date((firstDay / 1000 + 6 * 86400) * 1000);
return lastDay;
}
function getFirstDayOfMonth(date) {
if (!date) {
date = new Date();
}
var firstDay = new Date(date.getYear(), date.getMonth(), 1);
return firstDay;
}
function getLastDayOfMonth(date) {
if (!date) {
date = new Date();
}
var nextFirstDayOfMonth = new Date(date.getYear(), date.getMonth() + 1, 1);
var lastDay = new Date(nextFirstDayOfMonth - 86400000);
return lastDay;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment