Skip to content

Instantly share code, notes, and snippets.

@keelii
Created December 31, 2014 04:53
Show Gist options
  • Save keelii/37d61b0d0f831e616e42 to your computer and use it in GitHub Desktop.
Save keelii/37d61b0d0f831e616e42 to your computer and use it in GitHub Desktop.
Javascript check if sometime is current week
function isCurrentWeek(ts) {
var now = new Date();
now.setHours(0);
now.setMinutes(0);
now.setSeconds(0);
var nowTS = now.getTime();
var nowDay = getWeekNum(now.getDay());
function getWeekNum(week) {
return week === 0 ? 7 : week;
}
// 本周一
var startTS = nowTS - (nowDay - 1) * 24 * 60 * 60 * 1000;
var start = new Date(startTS);
var startDate = start.getDate();
// 本周日
var endTS = nowTS + (7 - nowDay) * 24 * 60 * 60 * 1000;
var end = new Date(endTS);
end.setHours(23);
end.setMinutes(59);
end.setSeconds(59);
var endDate = end.getDate();
if ( /isdebug/.test(location.href) ) {
console.log('Start:\t ' + startDate);
console.log('End:\t ' + endDate);
}
return ( ts >= start.getTime() && ts <= end.getTime() );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment