显示 if 语句的结果



>我的代码中有一个 if 语句,我似乎无法显示。 我一直无法定义(输出 3)。 这是确定距离下一个出生日期的天数的计算。 基本上,如果出生日期已经过去了,它会在出生日期上加一年并减去当前日期。 我将如何在 JS 代码顶部的语句中显示它以及按下"计算"按钮时显示的其他语句? 感谢!

这是主要的代码块,它让我知道如何显示它。

if (diff.cbdiff < 0 ) {
var date = new Date(currentBday); 
date.setFullYear(date.getFullYear() + 1);
document.getElementById('output3').innerHTML= ((date-date2)/(1000*60*60*24));
console.log(output3);
} else {
document.getElementById('output3').innerHTML=diff.cbdiff;
console.log(output3);
}

这是完整的JS代码:

function calc() {
var date1 = new Date();
var date2 = new Date(document.getElementById("date2").value);
var diff = dateDiff(date1, date2);
var output = "You are " + diff.year + " year" + "s " + diff.month + " month" + "s And " + diff.day + " day" + "s Old";
var output1 = "You are " + diff.days + " day" + "S" + "old";
var output2 = "You are " + diff.secondsalive + " seconds" + "old";
var output3 = "It is " + output3 + " days until your next Birthday";
document.getElementById("output").innerHTML = output;
document.getElementById("output1").innerHTML = output1;
document.getElementById("output2").innerHTML = output2;
document.getElementById("output3").innerHTML = output3;
console.log(date1);
console.log(date2);
}
function dateDiff(date1, date2) {
if (date1 > date2) return dateDiff(date2, date1);
var diff = {};
//console.log(currentBday);
diff.secondsalive = ((date2 - date1) / (1000)).toLocaleString({
undefined,
maximumFractionDigits: 1
});
diff.day = date2.getDate() - date1.getDate();
diff.days = ((date2 - date1) / (1000 * 60 * 60 * 24)).toFixed(1);
diff.year = date2.getFullYear() - date1.getFullYear();
diff.month = date2.getMonth() - date1.getMonth();
var currentBdYear = date2.getFullYear();
var currentBdMonth = date1.getMonth() + 1;
var currentBdDay = date1.getDate();
var currentYearBD = (diff.year + date2);
var addDate = new Date(diff.year);
//console.log(addDate);
var currentBday = new Date(currentBdMonth + "/" + currentBdDay + "/" + currentBdYear);
diff.cbdiff = ((currentBday - date2) / (1000 * 60 * 60 * 24)).toFixed(0);
console.log(diff.cbdiff);
if (diff.cbdiff < 0) {
var date = new Date(currentBday);
date.setFullYear(date.getFullYear() + 1);
document.getElementById('output3').innerHTML = ((date - date2) / (1000 * 60 * 60 * 24));
console.log(output3);
} else {
document.getElementById('output3').innerHTML = diff.cbdiff;
console.log(output3);
}
if (diff.day < 0) {
diff.month--;
var dayDiff = new Date(date2.getYear(), date2.getMonth(), 0).getDate() - date1.getDate();
diff.day = date2.getDate();
if (dayDiff > 0) {
diff.day += dayDiff;
}
}
if (diff.month < 0) {
diff.month += 12;
diff.year--;
}
return diff;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body id="gradient">
<div>
<h1>Birthday Calculator</h1>
<h2> Enter Birthday</h2>
<td>BirthDate:</td>
<input id="date2" value="10/1/74" />
<button onclick="calc()" style="height:50px">Calculate</button>
<br><br>
<table class="center">
<tr>
<td>
<div id="output"></div>
</td>
</tr>
<tr>
<td>
<div id="output1"></div>
</td>
</tr>
<tr>
<td>
<div id="output2"></div>
</td>
</tr>
<tr>
<td>
<div id="output3"></div>
</td>
</tr>
</table>
<p It is id="output3"></p>
<h3></h3>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

问题是这一行:

var output3 = "It is " + output3 + " days until your next Birthday";

output3是您要声明的变量,但它不包含距离您下一个生日的天数。那是在diff.cbdiff.

另一个问题是dateDiff()正在将更正的差异写入output3DIV。但是calc()使用diff.cbdiff覆盖它。因此,dateDiff()应该只将校正后的差异放在diff.cbdiff而不是DOM中。

function calc() {
var date1 = new Date();
var date2 = new Date(document.getElementById("date2").value);
var diff = dateDiff(date1, date2);
var output = "You are " + diff.year + " year" + "s " + diff.month + " month" + "s And " + diff.day + " day" + "s Old";
var output1 = "You are " + diff.days + " day" + "S" + "old";
var output2 = "You are " + diff.secondsalive + " seconds" + "old";
var output3 = "It is " + diff.cbdiff + " days until your next Birthday";
document.getElementById("output").innerHTML = output;
document.getElementById("output1").innerHTML = output1;
document.getElementById("output2").innerHTML = output2;
document.getElementById("output3").innerHTML = output3;
console.log(date1);
console.log(date2);
}
function dateDiff(date1, date2) {
if (date1 > date2) return dateDiff(date2, date1);
var diff = {};
//console.log(currentBday);
diff.secondsalive = ((date2 - date1) / (1000)).toLocaleString({
undefined,
maximumFractionDigits: 1
});
diff.day = date2.getDate() - date1.getDate();
diff.days = ((date2 - date1) / (1000 * 60 * 60 * 24)).toFixed(1);
diff.year = date2.getFullYear() - date1.getFullYear();
diff.month = date2.getMonth() - date1.getMonth();
var currentBdYear = date2.getFullYear();
var currentBdMonth = date1.getMonth() + 1;
var currentBdDay = date1.getDate();
var currentYearBD = (diff.year + date2);
var addDate = new Date(diff.year);
//console.log(addDate);
var currentBday = new Date(currentBdMonth + "/" + currentBdDay + "/" + currentBdYear);
diff.cbdiff = ((currentBday - date2) / (1000 * 60 * 60 * 24)).toFixed(0);
console.log(currentBday, diff.cbdiff);
if (diff.cbdiff < 0) {
var date = new Date(currentBday);
date.setFullYear(date.getFullYear() + 1);
diff.cbdiff = ((date - date2) / (1000 * 60 * 60 * 24)).toFixed(0);
console.log(diff.cbdiff);
}
if (diff.day < 0) {
diff.month--;
var dayDiff = new Date(date2.getYear(), date2.getMonth(), 0).getDate() - date1.getDate();
diff.day = date2.getDate();
if (dayDiff > 0) {
diff.day += dayDiff;
}
}
if (diff.month < 0) {
diff.month += 12;
diff.year--;
}
return diff;
}
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body id="gradient">
<div>
<h1>Birthday Calculator</h1>
<h2> Enter Birthday</h2>
<td>BirthDate:</td>
<input id="date2" value="10/1/74" />
<button onclick="calc()" style="height:50px">Calculate</button>
<br><br>
<table class="center">
<tr>
<td>
<div id="output"></div>
</td>
</tr>
<tr>
<td>
<div id="output1"></div>
</td>
</tr>
<tr>
<td>
<div id="output2"></div>
</td>
</tr>
<tr>
<td>
<div id="output3"></div>
</td>
</tr>
</table>
<p It is id="output3"></p>
<h3></h3>
<script type="text/javascript" src="script.js"></script>
</body>
</html>

output3 是未定义的,因为它不是一个变量。它是一个 html 元素的 id。您没有声明输出 3 是什么

最新更新