0

例如: 2013 年 11 月的第45 到 49周 ,2014 年 12 月的第49 到 53周

4

1 回答 1

0

这是德国/欧洲/ISO 周的解决方案。

  1. 查找给定日期的一周中的星期四。星期四确定所请求周所属的年份
  2. 找出当年 1 月 4 日那一周的星期四。这个星期四是第 1 周
  3. 计算当前周的星期四和第一周的星期四之间的周差加一......这将是给定日期的周数

尝试这个

function thursday(mydate) {
  var Th=new Date();
  Th.setTime(mydate.getTime() + (3-((mydate.getDay()+6) % 7)) * 86400000);
  return Th;
}

function getCalWeek(y, m, d) {
    thedate=new Date(y, m-1, d);
    ThursDate=thursday(thedate);
    weekYear=ThursDate.getFullYear();
    ThursWeek1=thursday(new Date(weekYear,0,4));
    theweek=Math.floor(1.5+(ThursDate.getTime()-ThursWeek1.getTime())/86400000/7);
    return theweek;
}

console.log("The week of the given date is: " + getCalWeek(2013, 11, 5));

编辑:对于您的具体问题,您需要给出月份和年份,然后计算周数

function daysInMonth(month,year) {
    var m = [31,28,31,30,31,30,31,31,30,31,30,31];
    if (month != 2) return m[month - 1];
    if (year%4 != 0) return m[1];
    if (year%100 == 0 && year%400 != 0) return m[1];
    return m[1] + 1;
}

function getWeekRange(m, y) {
    var startWeek = getCalWeek(y, m, 1);
    var endWeek = getCalWeek(y, m, daysInMonth(m, y));
    return startWeek + " to " + endWeek;
}
于 2013-11-05T16:23:04.790 回答