JavaScript 获取当前周数

书史足自悦,安用勤与劬。这篇文章主要讲述JavaScript 获取当前周数相关的知识,希望能为你提供帮助。
【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>


    推荐阅读