Created
December 31, 2014 04:53
-
-
Save keelii/37d61b0d0f831e616e42 to your computer and use it in GitHub Desktop.
Javascript check if sometime is current week
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
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