JavaScript 获取当前周数

Posted wx62d1485ecb778

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 获取当前周数相关的知识,希望对你有一定的参考价值。


<!DOCTYPE html>
<html>
<body>
<h2>
javascript函数
</h2>
<p>
本例调用了一个执行计算的函数,然后返回结果:
</p>
<p id="demo"></p>
<script>
function getWeek(dt)
// 默认当前时间
var d = dt ? new Date(dt) : new Date();
var day = d.getDay();
var originDate = d.getFullYear() + / + "01" + / + "01" + 00:00:00;
var nowDate = d.getFullYear() + / + ((d.getMonth() + 1) > 9 ? (d.getMonth() + 1) : 0 + (d.getMonth() + 1)) + "/" + (d.getDate() > 9 ? d.getDate() : 0 + d.getDate()) + 00:00:00;
var nowStrapTime = (new Date(nowDate)).getTime();
var originStrapTime = (new Date(originDate)).getTime();
var originDay = ((new Date(originStrapTime)).getDay()) == 0 ? 7 : ((new Date(originStrapTime)).getDay());
var nowDay = d.getDay() == 0 ? 7 : d.getDay();
var weeks = 0;
var resultStrap = 0;
if (nowDay > originDay)
resultStrap = nowStrapTime - (nowDay - originDay) * 1000 * 3600 * 24 - originStrapTime;
if (resultStrap > 0)
weeks = (resultStrap / (3600 * 1000 * 24 * 7)) + 1
else
weeks = 1

else
resultStrap = nowStrapTime + (originDay - nowDay) * 1000 * 3600 * 24 - originStrapTime;
weeks = (resultStrap / (3600 * 1000 * 24 * 7)) + 1

return weeks

document.getElementById("demo").innerHTML = getWeek();//当前时间
//document.getElementById("demo").innerHTML = getWeek(2021-12-31);//指定日期
</script>
</body>
</html>

以上是关于JavaScript 获取当前周数的主要内容,如果未能解决你的问题,请参考以下文章

从javascript中的天数获取天数、周数、月数

JavaScript 获取日期范围从一年中的周数

python中根据时间获取周数,通过周数获取时间

python代码根据时间获取周数(week of the year)

如果根据当前日期获取当前周日期和当前周日期

如何计算当前日期是当年的第几周