我具有提示用户输入日期和用户输入将来日期的功能。然后,它计算两个日期之间的时差,并输出天,小时,分钟和秒。
我想让此函数接受当前和将来两个参数,例如:function calcTime(current, future)
。然后我要它返回;天,小时,分钟和秒。
我不确定如何执行此操作,以及如何正确格式化我的calcTime(['2019' '11' '02' '30'], ['2020' '11' '02' '30']);
格式(或最好的方法,等等。)
这是我当前的功能:
function calcTime() {
//user input
var userYearCurrent = prompt("Enter a year in numbers(2019)", "Enter a year ex: 2019, 2020, 2021");
var userMonthCurrent = prompt("Enter a month in numbers(1-12), or type a month name", "Enter a month ex: 11, November");
var userDayCurrent = prompt("Enter a day of The month in mumbers(1-31)", "Enter a day of the month ex: 14, 21");
var userTimeStringCurrent = 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");
//split time
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]);
//user input
var userYearFuture = prompt("Enter a FUTURE year in numbers(2019)", "Enter a year ex: 2019, 2020, 2021");
var userMonthFuture = prompt("Enter a FUTURE month in numbers(1-12), or type a month name", "Enter a month ex: 11, November");
var userDayFuture = prompt("Enter a FUTURE day of The month in mumbers(1-31)", "Enter a day of the month ex: 14, 21");
var userTimeStringFuture = 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");
//split time
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);
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("time1").innerHTML = "Time between your two dates: " + days + " Days: " + hours + " Hours: " + minutes + " Minutes: " + seconds + " Seconds";
}
谢谢!!
您可以像这样调用该函数calcTime('14 / 9/2019','14/9/2020')
function calcTime(currentdate,futuredate) {
// var currentdate = '14/9/2019';
// var futuredate = '14/9/2020';
var userCurrentDatePart = currentdate.split('/');
var userFutureDatePart = futuredate.split('/');
//user input
var userYearCurrent = userCurrentDatePart[2];
var userMonthCurrent = userCurrentDatePart[1];
var userDayCurrent = userCurrentDatePart[0];
var userTimeStringCurrent = '0:0';
//split time
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]);
//user input
var userYearFuture = userFutureDatePart[2];
var userMonthFuture = userFutureDatePart[1];
var userDayFuture = userFutureDatePart[0];
var userTimeStringFuture = '23:59';
//split time
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);
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("time1").innerHTML = "Time between your two dates: " + days + " Days: " + hours + " Hours: " + minutes + " Minutes: " + seconds + " Seconds";
}
肯定有更好的方法来实现这一点,但是在您的情况下,您可以使用提示作为缺少日期部分的后备值:
function calcTime(currentDate=[], futureDate=[]) {
const [
userYearCurrent = prompt("Enter a year in numbers(2019)", "Enter a year ex: 2019, 2020, 2021"),
userMonthCurrent = prompt("Enter a month in numbers(1-12), or type a month name", "Enter a month ex: 11, November"),
userDayCurrent = prompt("Enter a day of The month in mumbers(1-31)", "Enter a day of the month ex: 14, 21"),
userTimeStringCurrent = 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")
] = currentDate;
const [
userYearFuture = prompt("Enter a FUTURE year in numbers(2019)", "Enter a year ex: 2019, 2020, 2021"),
userMonthFuture = prompt("Enter a FUTURE month in numbers(1-12), or type a month name", "Enter a month ex: 11, November"),
userDayFuture = prompt("Enter a FUTURE day of The month in mumbers(1-31)", "Enter a day of the month ex: 14, 21"),
userTimeStringFuture = 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")
] = futureDate;
//split time
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]);
//user input
//split time
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);
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";
}
console.log(calcTime(['2019','09','03','00:00'], ['2019','09',undefined,'00:00']));