gpt4 book ai didi

javascript - 连接每个元素中的日期

转载 作者:行者123 更新时间:2023-11-28 03:42:28 24 4
gpt4 key购买 nike

我想连接日期。这是我的代码,我首先创建了今天的年份,然后添加了 1 个月成为 2019 年的下一个日期。我不知道问题是什么,为什么它没有显示结果。

function myFunction() {

var d = new Date();
var n = d.getFullYear();
var date = document.getElementById("demo").innerHTML = n;

var d1 = new Date();
var n1 = d1.getFullYear()+1;
var date1 = document.getElementById("demo1").innerHTML = n1;

var str3 = "-";
var res = date.concat(str3,date1);
document.getElementById("disp").innerHTML = res;

}
<!DOCTYPE html>
<html>
<body onload="myFunction()">

<p>Click the button to display the full year of todays date.</p>


<p id="demo"></p>
<p id="demo1"></p>
<p id="disp"></p>



</body>
</html>

最佳答案

I don't know what the problem is why it didn't display the result.

因为你正在得到

"Uncaught TypeError: date.concat is not a function",

date 是一个数字,它没有 concat 方法。首先将其转换为 String

var res = String(date).concat(str3,date1);

演示

function myFunction() {

var d = new Date();
var n = d.getFullYear();
var date = document.getElementById("demo").innerHTML = n;

var d1 = new Date();
var n1 = d1.getFullYear()+1;
var date1 = document.getElementById("demo1").innerHTML = n1;
console.log(date, date1);
var str3 = "-";
var res = String(date).concat(str3,date1);
document.getElementById("disp").innerHTML = res;

}
<!DOCTYPE html>
<html>
<body onload="myFunction()">

<p>Click the button to display the full year of todays date.</p>


<p id="demo"></p>
<p id="demo1"></p>
<p id="disp"></p>



</body>
</html>

关于javascript - 连接每个元素中的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48823231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com