因此,我正試圖用JavaScript計算用戶輸入的兩個日期之間的差異;然而,我一直收到InvalidDate。如果有任何幫助,我們將不勝感激。
下面是我的代碼:
<html>
<head>
<title>作業計算器</title>
</head>/span>
<body>
<h1>作業計算器</h1>
<form>/span>
<label for="StartDate">Date to Start: </label>開始日期:</label>
< input id="StartDate" name="StartDate" type="date"/>
<label for="DueDate">Due Date: </label>
< input id="DueDate" name="DueDate" type="date"/>
< input type="submit" value="submit" onclick="displayDates()" >
</form>
<script type = "text/javascript" >
//獲得開始日期和到期日期
var startInput = document.getElementById("StartDate"/span>).value。
var dueInput = document.getElementById("DueDate").value。
var startDate = new Date(startInput)。
var dueDate = new Date(dueInput)。
//計算兩個日期的時間差。
var DifferenceInTime = dueDate.getTime() - startDate.getTime() 。
//要計算兩個日期之間的天數。
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24) 。
function displayDates() {
//T to display the of days a student has to work on the assignment (result)
document.write("Start your assignment by " startDate ".<br>"
"在" dueDate ".<br>"之前完成作業。
"你必須在這么多天內完成你的作業。" DifferenceInDays "。")。
}
</script>>
</body>
</html>
uj5u.com熱心網友回復:
幾乎所有的javscript代碼都需要移到內displayDates()函式。
當它在代碼之外時,它會立即運行,所以像startInput和dueInput這樣的變數將被設定一次,而不會再有。
你的目標是找出startInput和dueInput 在用戶點擊了按鈕之后。
uj5u.com熱心網友回復:
你在點擊按鈕時呼叫函式,但在頁面加載時從輸入欄位獲取值,即使資料值沒有被選中。 你應該把這部分也放在你在點擊按鈕時呼叫的函式中。
function displayDates() {
var startInput = document.getElementById("StartDate"/span>).value;
var dueInput = document.getElementById("DueDate").value。
var startDate = new Date(startInput)。
var dueDate = new Date(dueInput)。
//計算兩個日期的時間差。
var DifferenceInTime = dueDate.getTime() - startDate.getTime() 。
//要計算兩個日期之間的天數。
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24) 。
//T to display the days a student has to work on the assignment (result).
document.write("Start your assignment by " startDate ".<br>"
"在" dueDate ".<br>"完成你的作業。
"你必須在這么多天內完成你的作業。" DifferenceInDays "。")。
}
uj5u.com熱心網友回復:
你只需要將計算日期之間的差異的代碼移到displayDates函式中。
<!DOCTYPE html>
<html>
<head>
<title>作業計算器</title>
</head>/span>
<body>
<h1>作業計算器</h1>
<form>/span>
<label for="StartDate">Date to Start: </label>開始日期:</label>
< input id="StartDate" name="StartDate" type="date"/>
<label for="DueDate">Due Date: </label>
< input id="DueDate" name="DueDate" type="date"/>
< input type="submit" value="submit" onclick="displayDates()" >
</form>
<script type = "text/javascript" >
function displayDates() {
//獲得開始日期和到期日期。
var startInput = document.getElementById("StartDate"/span>).value;
var dueInput = document.getElementById("DueDate").value。
var startDate = new Date(startInput)。
var dueDate = new Date(dueInput)。
//計算兩個日期的時間差。
var DifferenceInTime = dueDate.getTime() - startDate.getTime() 。
//要計算兩個日期之間的天數。
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24) 。
//T to display the days a student has to work on the assignment (result).
document.write("Start your assignment by " startDate ".<br>"
"在" dueDate ".<br>"完成你的作業。
"你必須在這么多天內完成你的作業。"
DifferenceInDays "。")。)
}
</script>>
</body>
</html>/span>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" class="snippet-box-edit snippet-box-result" frameborder="0"></iframe>
uj5u.com熱心網友回復:
將函式外的所有JavaScript代碼放到displayDates函式中。
<script>
function displayDates(){
//獲得開始日期和到期日期。
var startInput = document.getElementById("StartDate"/span>).value;
var dueInput = document.getElementById("DueDate").value。
var startDate = new Date(startInput)。
var dueDate = new Date(dueInput)。
//計算兩個日期的時間差。
var DifferenceInTime = dueDate.getTime() - startDate.getTime() 。
//要計算兩個日期之間的天數。
var DifferenceInDays = DifferenceInTime / (1000 * 3600 * 24) 。
//T to display the days a student has to work on the assignment (result).
document.write("Start your assignment by " startDate ".<br>"
"在" dueDate ".<br>"完成你的作業。
"你必須在這么多天內完成你的作業。" DifferenceInDays "。")。
}
</script>
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/321648.html
標籤:
