Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态javaScript日期指向明天的日期_Javascript - Fatal编程技术网

动态javaScript日期指向明天的日期

动态javaScript日期指向明天的日期,javascript,Javascript,我正在使用下面的脚本创建动态日期 HTML: <span id="spanDate"></span> var months = ['01','02','03','04','05','06','07', '08','09','10','11','12']; var tomorrow = new Date(Date.now() + (1000*3600*24)); //tomorrow.setTime(tomor

我正在使用下面的脚本创建动态日期

HTML:

<span id="spanDate"></span>
var months = ['01','02','03','04','05','06','07',
        '08','09','10','11','12'];       
        var tomorrow = new Date(Date.now() + (1000*3600*24));
        //tomorrow.setTime(tomorrow.getTime() + (1000*3600*24));       
        document.getElementById("spanDate").innerHTML = ('00' + tomorrow.getDate()).slice(-2) + "/" + months[tomorrow.getMonth()] + "/" + tomorrow.getFullYear();


发生的情况是,当我呈现此代码时,它会显示从我的机器日期算起的第二天(即明天)的日期,而不会显示我今天的日期。为什么会发生这种情况?

因为您在初始化日期时添加了一天

 //You add 1000*3600*24 milliseconds: 1 day ===> outcome is tomorrow
 tomorrow.setTime(tomorrow.getTime() + (1000*3600*24)); 
试一试

小提琴:


也许将参数从“明天”更改为“今天”也是有用的;-)

这是因为在日期构造函数的当前时间中添加了一整天。如果要显示当前日期,请使用

var today = new Date();
替换

document.getElementById("spanDate").innerHTML = ('00' + tomorrow.getDate()).slice(-2) + "/" + months[tomorrow.getMonth()] + "/" + tomorrow.getFullYear();


预期产量是多少?您是否希望此预期输出应为今天的日期,该日期与我的机器日期类似,但我得到的是明天的日期,即我的机器提前的日期。您是否尝试过
var-tomory=new date()而不是
var明天=新日期(Date.now()+(1000*3600*24))?-1。你问的问题基本上与此相同:为什么
console.log(1+1)
log
2
而不是
1
?你看过你的代码了吗?如果你仔细阅读了你最有可能复制“你的”代码的地方,你就会知道提问者特别要求你的当前日期加上一天。你的努力+1。工作起来很有魅力。我会接受另一个人的回答,因为他先回答了。
document.getElementById("spanDate").innerHTML = ('00' + tomorrow.getDate()).slice(-2) + "/" + months[tomorrow.getMonth()] + "/" + tomorrow.getFullYear();
document.getElementById("spanDate").innerHTML = ('00' + today.getDate()).slice(-2) + "/" + months[today.getMonth()] + "/" + today.getFullYear();