簡體   English   中英

獲取兩個日期之間的時間適用於幾天和幾小時,但不是幾分鍾或幾秒

[英]Get time between two dates works for days and hours, but not minutes or seconds

我要求用戶提供兩個日期,一個是最近的,一個是將來的。

我的腳本應該能夠返回天、小時、分鍾和秒。

它成功地返回了天和小時,但分鍾和秒都返回 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構造函數。 見下文(為了這個例子,我刪除了提示並添加了 static 值):

 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();

對於您的示例,將日期構造函數參數完全設為yyyy-mm-ddThh:mm:ss

 function calcTime(current, future) { var userYearCurrent = (current[0] || prompt("Enter a year in numbers(2019)", "Enter a year ex: 2019, 2020, 2021")); var userMonthCurrent = (current[1] || prompt("Enter a month in numbers(1-12), or type a month name", "Enter a month ex: 11, November")); var userDayCurrent = (current[2] || prompt("Enter a day of The month in mumbers(1-31)", "Enter a day of the month ex: 14, 21")); var userTimeCurrent = (current[3] || 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 = (future[0] || prompt("Enter a FUTURE year in numbers(2019)", "Enter a year ex: 2019, 2020, 2021")); var userMonthFuture = (future[1] || prompt("Enter a FUTURE month in numbers(1-12), or type a month name", "Enter a month ex: 11, November")); var userDayFuture = (future[2] || prompt("Enter a FUTURE day of The month in mumbers(1-31)", "Enter a day of the month ex: 14, 21")); var userTimeFuture = (future[3] || 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}T${userTimeCurrent}`); var dateFuture = new Date(`${userYearFuture}-${userMonthFuture}-${userDayFuture}T${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("time").innerHTML = "Time between your two dates: " + days + " Days: " + hours + " Hours: " + minutes + " Minutes: " + seconds + " Seconds"; } calcTime(['2019', '01', '11', '02:15:06'], ['2020', '09', '06', '17:45:26'])
 <div id="time"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM