function getWeekNumber(d) {
// Copy date so don't modify original
d = new Date(d)
d.setHours(0,0,0)
// Set to nearest Thursday: current date + 4 - current day number
// Make Sunday's day number 7
d.setDate(d.getDate() + 4 - (d.getDay()||7))
// Get first day of year
var yearStart = new Date(d.getFullYear(),0,1)
// Calculate full weeks to nearest Thursday
var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7)
// Return array of year and week number
var getFullYear = d.getFullYear()
var getWeekNo = weekNo
return [getFullYear,getWeekNo]
}
如果你想测试一下,通过下边这段代码
alert(getWeekNumber(“2013-09-10″))
最后得到2013,37
function func(YearStr,MonthStr) {var time = YearStr + "/" + MonthStr + "/01"
var objtime= new Date(time)
var dayfitst = parseInt(objtime.getDay())//得到这个月的第一天是星期几 0(周日) 到 6(周六)
//如果是周日(一周的开始)
if (dayfitst == 0) {
objtime.setDate(8)
}
else {
//那么计算这一天到第二周的第一天相差多少天
var day = 7 - dayfitst
objtime.setDate(1 + day)//这里算出第二周第一天的日期
}
var beginStr = objtime.toLocaleDateString()
objtime.setDate(objtime.getDate() + 6)
var endStr = objtime.toLocaleDateString()
alert("次月的第二周的日期从" + beginStr + "到" + endStr)
}
<script>function getInfo(year, month) {
var d = new Date()
// what day is first day
d.setFullYear(year, month-1, 1)
var w1 = d.getDay()
if (w1 == 0) w1 = 7
// total day of month
d.setFullYear(year, month, 0)
var dd = d.getDate()
// first Monday
if (w1 != 1) d1 = 7 - w1 + 2
else d1 = 1
week_count = Math.ceil((dd-d1+1)/7)
document.write(year + "年" + month + "月有" + week_count +"周<br/>")
for (var i = 0 i < week_count i++) {
var monday = d1+i*7
var sunday = monday + 6
var from = year+"/"+month+"/"+monday
var to
if (sunday <= dd) {
to = year+"/"+month+"/"+sunday
} else {
d.setFullYear(year, month-1, sunday)
to = d.getFullYear()+"/"+(d.getMonth()+1)+"/"+d.getDate()
}
document.write("第"+(i+1)+"周 从" + from + " 到 " + to + "<br/>")
}
}
getInfo(2013,12)
</script>