首页 > 解决方案 > 添加日期更改脚本

问题描述

我有这段代码显示第二天的日期。是否可以更改日期的行为,使其在 17:00 更改?例如,现在代码显示日期 16.6.2020(这是正确的),17:00 之后应该显示 17.6.2020

document.querySelector("a[href='data']").setAttribute("id", "day");
document.querySelector("a[href='data2']").setAttribute("id", "day2");

var currentDate = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.getElementById("day").innerHTML = day + "." + month + "." + year;
document.getElementById("day2").innerHTML = day + "." + month + "." + year;
<a class="tn-atom" href="data" id=""></a>
<a class="tn-atom" href="data2" id=""></a>

标签: javascriptdate

解决方案


如果您不想弄乱时区,请将日期添加 7 小时,以便在 17:00 显示下一天的日期

var currentDate = new Date(new Date().getTime() + 31 * 60 * 60 * 1000);
console.log(currentDate)

// testing DST
const aroundDST = new Date(2020,2,29,0,0,0,0); // 29th of March 2020 - DST-SST in most of Europe
const afterDST = new Date(aroundDST.getTime() + 31 * 60 * 60 * 1000);
console.log(afterDST)


推荐阅读