1 小时数得保证是正整数,开始时间保证是合法的时间格式
2 代码
function count() {var hours = document.getElementById("xiaoshishu").value
var startDate = document.getElementById("kaishi").value
hours = parseInt(hours)
startDate = new Date(startDate)
var newDate = new Date(startDate.setHours(startDate.getHours() + hours))
document.getElementById("jieshu").value = `${newDate.getFullYear()}-${newDate.getMonth() + 1}-${newDate.getDate()} ${newDate.getHours()}:${newDate.getMinutes()}`
}
count()
<!DOCTYPE HTML><html>
<head>
<meta charset=UTF-8>
<title>recursion</title>
<style type="text/css">
</style>
<script type="text/javascript">
onload = function ()
{
setInterval (function ()
{
var date = new Date ()
date.setHours (date.getHours () + 1)
var m = date.getMonth () + 1
m = m < 10 ? "0" + m : m
var d = date.getDate ()
d = d < 10 ? "0" + d : d
var h = date.getHours ()
h = h < 10 ? "0" + h : h
var mm = date.getMinutes ()
mm = mm < 10 ? "0" + mm : mm
txt.value = m + "-" + d + "-" + h + "-" + mm
}, 1000)
}
</script>
</head>
<body>
<input type="text" id="txt" />
</body>
</html>