为什么 JavaScript 代码不会计算两个 HTML 日期时间输入之间的月份?
Why won't JavaScript code calculate months between two HTML date time inputs?
我正在尝试计算两个 HTML 日期时间输入之间的月数。
这意味着当用户从下拉日历中选择 7/21/21 作为开始日期并选择 8/21/21 作为结束日期时,预期结果是一个月。
我的实际结果没有显示在屏幕上。
我没有收到任何错误消息,所以我不知道要修复什么。
我尝试通过在开始日期 (movein-input
) 和结束日期 (moveout-input
) 上使用 Date.parse().value
并计算以毫秒为单位的时间间隔来计算两个日期之间的月差(毫秒)、分钟、小时、日、月、年、年、月、日和小时,但它确实有效。我在 youtube (https://www.youtube.com/watch?v=Q3oiSwdGAq8) and w3schools (https://www.w3schools.com/jsref/jsref_parse.asp) 上找到了这个想法。
W3schools 说当你在 HTML 中显示日期对象时,它会自动转换为字符串,所以我想我可以在 Date.Parse().value
.
中使用 html 日期输入
这是我的JavaScript代码
function calculatePrice() {
let d1 = document.getElementById("movein-input").value;
let d2 = document.getElementById("moveout-input").value;
let msBetweenD1And1970 = Date.parse(d1);
let msBetweenD2And1970 = Date.parse(d2);
let timeInMs = msBetweenD2And1970-msBetweenD1And1970;
let ms = timeInMs;
let second = 1000;
let minute = second*60;
let hour = minute*60;
let day = hour*24;
let month = day*30;
let year = day*365;
let years = Math.round(ms/year);
let months = years*12;
var t1 = "That is : "+months;
document.getElementById("m").innerHTML=t1
}
我也有html代码
<h2><span id="m">$</span></h2>
计算价格在按钮中
<button onclick="calculatePrice()">Enter</button>
当你需要除法时,你在乘法。
let months = Math.round(ms / 1000 / 60 / 60 / 24 / 30)
function calculatePrice() {
let d1 = document.getElementById("movein-input").value;
let d2 = document.getElementById("moveout-input").value;
let msBetweenD1And1970 = Date.parse(d1);
let msBetweenD2And1970 = Date.parse(d2);
let timeInMs = msBetweenD2And1970-msBetweenD1And1970;
let ms = timeInMs;
let months = Math.round(ms / 1000 / 60 / 60 / 24 / 30)
var t1 = `That is ${months} months`;
document.getElementById("m").textContent=t1
}
<h2><span id="m">$</span></h2>
<input type='date' id='movein-input' value='2021-03-01' />
<input type='date' id='moveout-input' value='2021-06-01' />
<button onclick='calculatePrice()'>calculate</button>
我正在尝试计算两个 HTML 日期时间输入之间的月数。
这意味着当用户从下拉日历中选择 7/21/21 作为开始日期并选择 8/21/21 作为结束日期时,预期结果是一个月。
我的实际结果没有显示在屏幕上。
我没有收到任何错误消息,所以我不知道要修复什么。
我尝试通过在开始日期 (movein-input
) 和结束日期 (moveout-input
) 上使用 Date.parse().value
并计算以毫秒为单位的时间间隔来计算两个日期之间的月差(毫秒)、分钟、小时、日、月、年、年、月、日和小时,但它确实有效。我在 youtube (https://www.youtube.com/watch?v=Q3oiSwdGAq8) and w3schools (https://www.w3schools.com/jsref/jsref_parse.asp) 上找到了这个想法。
W3schools 说当你在 HTML 中显示日期对象时,它会自动转换为字符串,所以我想我可以在 Date.Parse().value
.
这是我的JavaScript代码
function calculatePrice() {
let d1 = document.getElementById("movein-input").value;
let d2 = document.getElementById("moveout-input").value;
let msBetweenD1And1970 = Date.parse(d1);
let msBetweenD2And1970 = Date.parse(d2);
let timeInMs = msBetweenD2And1970-msBetweenD1And1970;
let ms = timeInMs;
let second = 1000;
let minute = second*60;
let hour = minute*60;
let day = hour*24;
let month = day*30;
let year = day*365;
let years = Math.round(ms/year);
let months = years*12;
var t1 = "That is : "+months;
document.getElementById("m").innerHTML=t1
}
我也有html代码
<h2><span id="m">$</span></h2>
计算价格在按钮中
<button onclick="calculatePrice()">Enter</button>
当你需要除法时,你在乘法。
let months = Math.round(ms / 1000 / 60 / 60 / 24 / 30)
function calculatePrice() {
let d1 = document.getElementById("movein-input").value;
let d2 = document.getElementById("moveout-input").value;
let msBetweenD1And1970 = Date.parse(d1);
let msBetweenD2And1970 = Date.parse(d2);
let timeInMs = msBetweenD2And1970-msBetweenD1And1970;
let ms = timeInMs;
let months = Math.round(ms / 1000 / 60 / 60 / 24 / 30)
var t1 = `That is ${months} months`;
document.getElementById("m").textContent=t1
}
<h2><span id="m">$</span></h2>
<input type='date' id='movein-input' value='2021-03-01' />
<input type='date' id='moveout-input' value='2021-06-01' />
<button onclick='calculatePrice()'>calculate</button>