gpt4 book ai didi

javascript - 没有。在 javascript 中给定一年的几周

转载 作者:行者123 更新时间:2023-11-30 06:28:13 25 4
gpt4 key购买 nike

这是我用来计算周数的 javscript 代码。从给定日期开始....但我得到的输出不正确

   var dayNr   = (this.getDay() + 6) % 7;  

target.setDate(target.getDate() - dayNr + 3);


var firstThursday = target.valueOf();

target.setMonth(0, 1);

if (target.getDay() != 4) {
target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
}

return 1 + Math.ceil((firstThursday - target) / 604800000);

我想计算周数。从给定日期开始......也欢迎任何其他解决方案......急需

最佳答案

function getWeekNumber(d) {
// Copy date so don't modify original
var 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
return [d.getFullYear(), weekNo];
}

Refrence Answer

关于javascript - 没有。在 javascript 中给定一年的几周,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19630232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com