Created
January 1, 2015 08:36
-
-
Save keelii/e35d56f8afbe447f4564 to your computer and use it in GitHub Desktop.
some in which week, last N 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 inSomeWeek(ts, n) { | |
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; | |
} | |
// 几周内 | |
n = n || 1; | |
// 本周一 - n周毫秒数 | |
var startTS = nowTS - (nowDay - 1) * 24 * 60 * 60 * 1000 - (n-1) * 7 * 24 * 60 * 60 * 1000; | |
var start = new Date(startTS); | |
var startDate = start.getDate(); | |
// 本周日 + n周毫秒数 | |
var endTS = nowTS + (7 - nowDay) * 24 * 60 * 60 * 1000 + (n-1) * 7 * 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