我是编程新手,所以请注意。还在学习。
我必须减去两个日期才能得到它们之间的差。
这是我到目前为止所拥有的:
<script type="text/javascript">
function validateDate() {
var date = document.getElementById ("userDate").value;
var regex= /^\d{2}\/\d{2}\/\d{4}$/;
if (regex.test (date)) {
document.getElementById("datePrompt").innerHTML="<img src=\"yes.png\">";
return (true);
}
else {
document.getElementById("datePrompt").innerHTML="<img src=\"no.png\">  : Enter in the format of mm/dd/yyyy";
return (false);
}
}
function calcDate () {
var today = new Date();
var date = document.getElementById ("userDate").value;
var dd = String(today.getDate()).padStart(2, '0');
var mm = String(today.getMonth() + 1).padStart(2, '0');
var yyyy = today.getFullYear();
var today = mm + '/' + dd + '/' + yyyy;
document.getElementById("today").innerHTML="Your Input: " + date + " - Today is: " + today +" <br>" ;
}
function dayDiff(date, today) {
d1 = today.getTime() / 86400000;
d2 = date.getTime() / 86400000;
return new Number(d2 - d1).toFixed(0);
}
</script>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<body>
<p>
<input name="userDate" id="userDate" type="text" size="20" maxlength="15" placeholder="mm/dd/yyyy" onblur="validateDate();"> <span id="datePrompt">1) Input Date</span> <br>
<button type="submit" onclick="calcDate();">Calculate Date</button> <br>
<span id="today" style="background-color: darkslateblue; color:white;"></span>
<span id="dateDiff" style="background-color: blue; color:white;" ></span>
</p>
</body>
</html>
我似乎不太明白该函数减去用户输入的日期和今天的日期。