我要问用户两个日期,一个是最近的,另一个是将来的。
我的脚本应该能够返回几天,几小时,几分钟和几秒钟。
它成功地返回了几天和几小时,但是在几分钟和几秒钟内都返回0,这是我在做什么错?
谢谢!!
function calcTime()
{
var userYearCurrent = parseInt(prompt("Enter a year in numbers(2019)","Enter a year ex: 2019, 2020, 2021"));
var userMonthCurrent = parseInt(prompt("Enter a month in numbers(1-12), or type a month name","Enter a month ex: 11, November"));
var userDayCurrent = parseInt(prompt("Enter a day of The month in mumbers(1-31)","Enter a day of the month ex: 14, 21"));
var userTimeCurrent = parseInt(prompt("Enter a time (2:05) or if seconds are needed(2:05:30)","Enter a time of day ex: 11:15, do not use military time"));
var userYearFuture = parseInt(prompt("Enter a FUTURE year in numbers(2019)","Enter a year ex: 2019, 2020, 2021"));
var userMonthFuture = parseInt(prompt("Enter a FUTURE month in numbers(1-12), or type a month name","Enter a month ex: 11, November"));
var userDayFuture = parseInt(prompt("Enter a FUTURE day of The month in mumbers(1-31)","Enter a day of the month ex: 14, 21"));
var userTimeFuture = parseInt(prompt("Enter a FUTURE time (2:05) or if seconds are needed(2:05:30)","Enter a time of day ex: 11:15, do not use military time"));
var dateCurrent = new Date(userYearCurrent, userMonthCurrent, userDayCurrent, userTimeCurrent);
var dateFuture = new Date(userYearFuture, userMonthFuture, userDayFuture, userTimeFuture);
var mathSeconds = Math.abs(dateFuture.getTime() - dateCurrent.getTime()) / 1000;
var days = Math.floor(mathSeconds / 86400);
mathSeconds -= days * 86400;
var hours = Math.floor(mathSeconds / 3600) % 24;
mathSeconds -= hours * 3600;
var minutes = Math.floor(mathSeconds / 60) % 60;
mathSeconds -= minutes * 60;
var seconds = mathSeconds;
document.getElementById("time2").innerHTML = "Time between your two dates: " + days + " Days: " + hours + " Hours: " + minutes + " Minutes: " + seconds + " Seconds";
}
最佳答案
问题是您正在调用类似parseInt('2:05:30')
之类的东西,却无法满足您的需求。您需要分别解析每个片段,然后将小时,分钟和秒作为单独的参数传递给每个Date
构造函数。参见下文(出于本示例的考虑,我删除了提示并添加了静态值):
function promptTime()
{
var userYearCurrent = 2019;
var userMonthCurrent = 10;
var userDayCurrent = 2;
var userTimeStringCurrent = '2:05:23';
var userTimeStringCurrentParts = userTimeStringCurrent.split(':');
if (userTimeStringCurrentParts.length === 2) {
userTimeStringCurrentParts.push('00'); // empty seconds
}
var userHoursCurrent = parseInt(userTimeStringCurrentParts[0]);
var userMinutesCurrent = parseInt(userTimeStringCurrentParts[1]);
var userSecondsCurrent = parseInt(userTimeStringCurrentParts[2]);
var userYearFuture = 2021;
var userMonthFuture = 8;
var userDayFuture = 14;
var userTimeStringFuture = '6:37';
var userTimeStringFutureParts = userTimeStringFuture.split(':');
if (userTimeStringFutureParts.length === 2) {
userTimeStringFutureParts.push('00'); // empty seconds
}
var userHoursFuture = parseInt(userTimeStringFutureParts[0]);
var userMinutesFuture = parseInt(userTimeStringFutureParts[1]);
var userSecondsFuture = parseInt(userTimeStringFutureParts[2]);
var dateCurrent = new Date(userYearCurrent, userMonthCurrent, userDayCurrent, userHoursCurrent, userMinutesCurrent, userSecondsCurrent);
var dateFuture = new Date(userYearFuture, userMonthFuture, userDayFuture, userHoursFuture, userMinutesFuture, userSecondsFuture);
console.log(calcTime(dateCurrent, dateFuture));
}
function calcTime(dateCurrent, dateFuture)
{
var mathSeconds = Math.abs(dateFuture.getTime() - dateCurrent.getTime()) / 1000;
var days = Math.floor(mathSeconds / 86400);
mathSeconds -= days * 86400;
var hours = Math.floor(mathSeconds / 3600) % 24;
mathSeconds -= hours * 3600;
var minutes = Math.floor(mathSeconds / 60) % 60;
mathSeconds -= minutes * 60;
var seconds = mathSeconds;
return "Time between your two dates: " + days + " Days: " + hours + " Hours: " + minutes + " Minutes: " + seconds + " Seconds";
}
promptTime();
关于javascript - 获取两个日期之间的时间只能花费几天和几小时,而不能花费几分钟或几秒钟,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58211509/