用 Javascript 显示周数?

IT技术 javascript date calendar
2021-01-19 10:24:35

我有以下代码用于显示当天的名称,后跟固定短语。

<script type="text/javascript"> 
    <!-- 
    // Array of day names
    var dayNames = new Array(
    "It's Sunday, the weekend is nearly over",
    "Yay! Another Monday",
     "Hello Tuesday, at least you're not Monday",
     "It's Wednesday. Halfway through the week already",
     "It's Thursday.",
     "It's Friday - Hurray for the weekend",
    "Saturday Night Fever");
    var now = new Date();
    document.write(dayNames[now.getDay()] + ".");
     // -->
</script>

我想做的是在短语后面的括号中包含当前周数。我发现了以下代码:

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

取自http://javascript.about.com/library/blweekyear.htm但我不知道如何将它添加到现有的 javascript 代码中。

6个回答

只需将其添加到您当前的代码中,然后调用 (new Date()).getWeek()

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

    var weekNumber = (new Date()).getWeek();

    var dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    var now = new Date();
    document.write(dayNames[now.getDay()] + " (" + weekNumber + ").");
</script>
谢谢!这有效,但由于某种原因,它显示当前周数为 42,但所有在线资源都显示第 41 周。我已经检查了服务器和计算机上的时间/日期,但它们都是正确的。有任何想法吗?
2021-03-16 10:24:35
这是错误的:var onejan = new Date(this.getFullYear(), 0, 1) 那是因为,根据 ISO,一年中的第一周是第一个星期四。或者,换句话说,有 1 月 4 日的那一周。这解决了问题:var onejan = new Date(this.getFullYear(), 0, 4) 这也意味着onejan该变量的名称不正确。
2021-03-19 10:24:35
如果我将第四行更改为 'var onejan = new Date(this.getFullYear(),0,2);' 这是正确的。这是正确的做法吗?
2021-03-20 10:24:35
如果您的一周不是从周日开始,您可能想使用这个:stackoverflow.com/questions/6117814/...
2021-03-20 10:24:35
首先,你要问自己,如果你想要相当于strftime的 %U%V%W我认为人们通常使用 ISO 8601 周数 ( %V),其中第 1 周是该年的第一个星期四。
2021-03-31 10:24:35

考虑使用我的“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(包括测试)

first_monday_year 未使用
2021-03-15 10:24:35
对了,这个功能不行。现在显示的是第 18 周,而实际上是第 19 周。
2021-03-20 10:24:35
现在它说是第 40 周,这是错误的(现在是第 41 周)。不是真的“比其他人更准确”......
2021-03-24 10:24:35
basteln 和 this.lau_,这是因为此代码计算美国周数,而不是 ISO 标准周数。
2021-04-11 10:24:35

如果您已经使用 jquery-ui(特别是 datepicker):

Date.prototype.getWeek = function () { return $.datepicker.iso8601Week(this); }

用法:

var myDate = new Date();
myDate.getWeek();

更多信息: UI/Datepicker/iso8601Week

我意识到这不是一个通用的解决方案,因为它会产生依赖性。然而,考虑到 jquery-ui 的流行,这可能只是一个简单的适合某人 - 就像对我一样。

如果您想要一些有效且面向未来的东西,请使用像 MomentJS 这样的库。

moment(date).week();
moment(date).isoWeek()

http://momentjs.com/docs/#/get-set/week/

这应该是最佳答案。像上面建议的那样修改 Date 的原型是一种非常糟糕的做法。
2021-03-21 10:24:35

看起来我在weeknumber.net找到的这个函数非常准确且易于使用。

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: http://weeknumber.net/how-to/javascript 

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}

如果你像我一样幸运并且需要找到一个月的周数,稍微调整一下就可以了:

// Returns the week in the month of the date.
Date.prototype.getWeekOfMonth = function() {
  var date = new Date(this.getTime());
  date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), date.getMonth(), 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7);
}