JS獲取 當前時間是本年的第幾天? 第幾周?


<html>

<head>
</head>

<body>
  <div>現在是今年的<span id="daytotal"></span>天————————第<span id="weektotal"></span>周</div>
</body>
<script>
  //1.獲取本年已經走過的時間   天數
  var d1 = Math.ceil((new Date() - new Date(new Date().getFullYear().toString())) / (24 * 60 * 60 * 1000));
  //天數
  console.log(d1, "天數");
  //2. 代入獲取本年的1月1號是星期幾
  var week = new Date(new Date().getFullYear().toString() + "/01/01").getDay();
  console.log(week, "代入獲取本年的1月1號是星期幾");
  //計算 當前時間是本年的第幾周
  var total = Math.ceil((d1 + week - 1) / 7);
  //
  console.log(total, "計算 當前時間是本年的第幾周")
  document.getElementById('weektotal').innerHTML = total;
  document.getElementById('daytotal').innerHTML = d1;
</script>

</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM