JavaScript Date.getWeek()?

IT技术 javascript date
2021-03-14 12:37:52

我正在寻找一个经过测试的可靠解决方案,用于获取指定日期的当前周。我能找到的只是那些没有考虑闰年或完全错误的。有人有这种类型的东西吗?

或者甚至更好的函数说明一个月占用多少周。它通常是 5,但可以是 4(二月)或 6(第 1 天是星期日,一个月有 30-31 天)

================== 更新:

仍然不确定获得第 # 周,但由于我发现它不能解决我计算一个月占用多少周的问题,我放弃了它。

这是一个函数,用于找出月份在日历上占据了多少周:

getWeeksNum: function(year, month) {
    var daysNum = 32 - new Date(year, month, 32).getDate(),
        fDayO = new Date(year, month, 1).getDay(),
        fDay = fDayO ? (fDayO - 1) : 6,
        weeksNum = Math.ceil((daysNum + fDay) / 7);
    return weeksNum;
}
6个回答
/**
 * Returns the week number for this date.  dowOffset is the day of week the week
 * "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
 * the week returned is the ISO 8601 week number.
 * @param int dowOffset
 * @return int
 */
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */

    dowOffset = typeof(dowOffset) == 'number' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() - 
    (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;
    //if the year starts before the middle of a week
    if(day < 4) {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if(weeknum > 52) {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
              the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    }
    else {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

用法:

var mydate = new Date(2011,2,3); // month number starts from 0
// or like this
var mydate = new Date('March 3, 2011');
alert(mydate.getWeek());

来源

对不起,我搞砸了日期。2015年是问题年。无论如何,我调试了问题。dowOffset 数字正在运行typeof == 'int',应该是typeof == 'number'. 进行更改后,它会根据 ISO 8601 标准工作。要查看问题,您可以编辑此小提琴以使用“int”而不是“number”,您会看到 2014 年有 53 周,而实际上应该有 52 周。这使 2015 年全年都推迟了一周。这个问题每十年都会出现一次(2025、2035、n+10) - jsfiddle.net/2PBcS
2021-04-23 12:37:52
对于背景证据,关于 typeof 的 MDN 文档 - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/...以及 ISO 标准 8601 - en.wikipedia.org/wiki/ISO_8601#Week_dates
2021-05-13 12:37:52
typeof(dowOffset) == 'int'永远是false,也许你的意思是typeof dowOffset == 'number'?。
2021-05-13 12:37:52
Epochconverter 向我展示了相同的结果 - 第 1 周 = 2013 年 12 月 30 日。如果您的一周从星期日开始,它可以关闭一天。
2021-05-17 12:37:52
当我将使用您的代码与 epochconverter.com 的结果进行比较时,这似乎并不完全正确。请参阅此 Fiddle 并比较您的 2014 年第 1 周与 epochconverter 的第 1 周。您差一个?jsfiddle.net/q96AM/2
2021-05-18 12:37:52

对于那些寻求更简单方法的人;

Date.prototype.getWeek = function() {
  var onejan = new Date(this.getFullYear(),0,1);
  var today = new Date(this.getFullYear(),this.getMonth(),this.getDate());
  var dayOfYear = ((today - onejan + 86400000)/86400000);
  return Math.ceil(dayOfYear/7)
};

与:

var today = new Date();
var currentWeekNumber = today.getWeek();
console.log(currentWeekNumber);
没有按预期工作。try var today = new Date('2020-10-12T10:45:03.859Z');现在本周没有,42但它正在显示41
2021-05-05 12:37:52
出于某种原因,即使当前周是 1,这也给了我今天的第 2 周。
2021-05-16 12:37:52

考虑使用我的“Date.prototype.getWeek”实现,认为比我在这里看到的其他人更准确:)

Date.prototype.getWeek = function(){
    // We have to compare against the first monday of the year not the 01/01
    // 60*60*24*1000 = 86400000
    // 'onejan_next_monday_time' reffers to the miliseconds of the next monday after 01/01

    var day_miliseconds = 86400000,
        onejan = new Date(this.getFullYear(),0,1,0,0,0),
        onejan_day = (onejan.getDay()==0) ? 7 : onejan.getDay(),
        days_for_next_monday = (8-onejan_day),
        onejan_next_monday_time = onejan.getTime() + (days_for_next_monday * day_miliseconds),
        // If one jan is not a monday, get the first monday of the year
        first_monday_year_time = (onejan_day>1) ? onejan_next_monday_time : onejan.getTime(),
        this_date = new Date(this.getFullYear(), this.getMonth(),this.getDate(),0,0,0),// This at 00:00:00
        this_time = this_date.getTime(),
        days_from_first_monday = Math.round(((this_time - first_monday_year_time) / day_miliseconds));

    var first_monday_year = new Date(first_monday_year_time);

    // We add 1 to "days_from_first_monday" because if "days_from_first_monday" is *7,
    // then 7/7 = 1, and as we are 7 days from first monday,
    // we should be in week number 2 instead of week number 1 (7/7=1)
    // We consider week number as 52 when "days_from_first_monday" is lower than 0,
    // that means the actual week started before the first monday so that means we are on the firsts
    // days of the year (ex: we are on Friday 01/01, then "days_from_first_monday"=-3,
    // so friday 01/01 is part of week number 52 from past year)
    // "days_from_first_monday<=364" because (364+1)/7 == 52, if we are on day 365, then (365+1)/7 >= 52 (Math.ceil(366/7)=53) and thats wrong

    return (days_from_first_monday>=0 && days_from_first_monday<364) ? Math.ceil((days_from_first_monday+1)/7) : 52;
}

您可以在此处查看我的公共存储库https://bitbucket.org/agustinhaller/date.getweek(包括测试)

这最终不符合 ISO 8601。如果查看 2015 年的周数,应该有 53 周,但此函数返回 52。您还将看到该年的所有周数都减少了一个。jsfiddle.net/2PBcS/1
2021-05-08 12:37:52

获取周数

Date.prototype.getWeek = function() {
    var dt = new Date(this.getFullYear(),0,1);
    return Math.ceil((((this - dt) / 86400000) + dt.getDay()+1)/7);
};

var myDate = new Date(2013, 3, 25); // 2013, 25 April

console.log(myDate.getWeek());
不确定这个解决方案是否非常准确。这是一个小提琴。它似乎对什么是给定年份的最后一周和第一周感到困惑。jsfiddle.net/q96AM/1
2021-05-04 12:37:52
此日期返回 53 var myDate = new Date(2020, 11, 28); // 53
2021-05-18 12:37:52

我知道这是一个老问题,但也许它有帮助:

http://weeknumber.net/how-to/javascript