使用JavaScript计算时间差

17
我有两个HTML输入框,需要在JavaScript onBlur事件中计算时间差(因为我需要实时计算),并将结果插入到新的输入框中。
格式示例:10:00和12:30需要给出02:30。
谢谢!

你是在使用JavaJScript还是Javascript?它们都是不同的编程语言。 - Paul
2
“javascript” 不是 “java”的脚本语言! - gdoron
文本框中的日期是实际日期吗?您能发布HTML和一些示例数据吗? - Mathew Thompson
12个回答

49

这里是一个可能的解决方案:

function diff(start, end) {
    start = start.split(":");
    end = end.split(":");
    var startDate = new Date(0, 0, 0, start[0], start[1], 0);
    var endDate = new Date(0, 0, 0, end[0], end[1], 0);
    var diff = endDate.getTime() - startDate.getTime();
    var hours = Math.floor(diff / 1000 / 60 / 60);
    diff -= hours * 1000 * 60 * 60;
    var minutes = Math.floor(diff / 1000 / 60);

    // If using time pickers with 24 hours format, add the below line get exact hours
    if (hours < 0)
       hours = hours + 24;

    return (hours <= 9 ? "0" : "") + hours + ":" + (minutes <= 9 ? "0" : "") + minutes;
}

演示:http://jsfiddle.net/KQQqp/


我想在这里显示秒数。我该怎么做? - Chinmay235
出色的解决方案,完美地满足了我的需求。甚至可以计算跨越午夜的小时数。 - Tim

6

简而言之

一次性运行

const t1 = new Date(1579876543210) // your initial time
const t2 = new Date(1579987654321) // your later time

const diff = t2-t1
const SEC = 1000, MIN = 60 * SEC, HRS = 60 * MIN
const humanDiff = `${Math.floor(diff/HRS)}:${Math.floor((diff%HRS)/MIN).toLocaleString('en-US', {minimumIntegerDigits: 2})}:${Math.floor((diff%MIN)/SEC).toLocaleString('en-US', {minimumIntegerDigits: 2})}.${Math.floor(diff % SEC).toLocaleString('en-US', {minimumIntegerDigits: 4, useGrouping: false})}`

console.log("humanDiff:", humanDiff)
// > humanDiff: 30:51:51.0111

作为函数

function humanDiff (t1, t2) {
  const diff = Math.max(t1,t2) - Math.min(t1,t2) 
  const SEC = 1000, MIN = 60 * SEC, HRS = 60 * MIN
  
  const hrs = Math.floor(diff/HRS)
  const min = Math.floor((diff%HRS)/MIN).toLocaleString('en-US', {minimumIntegerDigits: 2})
  const sec = Math.floor((diff%MIN)/SEC).toLocaleString('en-US', {minimumIntegerDigits: 2})
  const ms = Math.floor(diff % SEC).toLocaleString('en-US', {minimumIntegerDigits: 4, useGrouping: false})
  
  return `${hrs}:${min}:${sec}.${ms}`
}

const t1 = new Date(1579876543210)
const t2 = new Date(1579987654321)

console.log("humanDiff(t1, t2):", humanDiff(t1, t2))
// > humanDiff: 30:51:51.0111

解释

根据您的最大和最小可报告增量以及格式需求,调整 humanDiff

const t1 = new Date(1579876543210) // Set your initial time (`t1`)
const t2 = new Date(1579986654321) // , conclusion time (`t2`), and
const diff = t2-t1 // calculate their difference in milliseconds
console.log("         t2:", t2.toISOString()) // >   t2: 2020-01-25T21:27:34.321Z
console.log("         t1:", t1.toISOString()) // >   t1: 2020-01-24T14:35:43.210Z
console.log("       diff:", diff)             // > diff: 111111111

// Set your constant time values for easy readability
const SEC = 1000
const MIN = 60 * SEC
const HRS = 60 * MIN

/* For a given unit
1) disregard any previously relevant units, e.g. to calculate minutes, we can
   disregard all hours & focus on only the remainder - `(diff%HRS)`
2) divide the remainder by the given unit, e.g. for minutes, `(diff%HRS)/MIN`
3) disregard any remainder, e.g. again for minutes, `Math.floor((diff%HRS)/MIN)`
NOTE: for your maximum unit (HRS in the examples below) you probably _don't_
  want to disregard high values, e.g. If the difference is >24 hrs and something,
  you should either include a DAYS value, or simply display 30 hrs */
let hrs = Math.floor(diff/HRS)
let min = Math.floor((diff%HRS)/MIN)
let sec = Math.floor((diff%MIN)/SEC)
let ms  = Math.floor(diff % SEC) // just the remainder 
          // BUT ms IS NOT ACTUALLY CORRECT, see humanDiff_3 for the fix ;-)
let humanDiff_1 = `${hrs}:${min}:${sec}.${ms}`
console.log("humanDiff_1:", humanDiff_1)
// > humanDiff_1: 30:51:51.111

sec = Math.round((diff%MIN)/SEC) // can also just round the last unit
const humanDiff_2 = `${hrs} hrs ${min} mins & ${sec} secs`
console.log("humanDiff_2:", humanDiff_2)
// > humanDiff_2: 30 hrs 51 mins & 51 secs

/* To ensure a set number of digits, format the numbers with `toLocaleString`'s
   `minimumIntegerDigits`, if more than 3 digits, also use its `useGrouping`   */
hrs = Math.floor(diff/HRS)
min = Math.floor((diff%HRS)/MIN).toLocaleString('en-US', {minimumIntegerDigits: 2})
sec = Math.floor((diff%MIN)/SEC).toLocaleString('en-US', {minimumIntegerDigits: 2})
ms = Math.floor(diff % SEC).toLocaleString('en-US', {minimumIntegerDigits: 4, useGrouping: false})

const humanDiff_3 = `${hrs}:${min}:${sec}.${ms}`
console.log("humanDiff_3:", humanDiff_3)
// > humanDiff_3: 30:51:51.0111
// NOTE: milliseconds are now 4 digits


1
感谢您提供这个函数,为什么SEC = 1000? - Danish
SEC = 1000 只是因为1秒由1000毫秒组成;毫秒是Javascript中日期的基本值。 - MetaSean

4
尝试这个。
var dif = ( new Date("1970-1-1 " + end-time) - new Date("1970-1-1 " + start-time) ) / 1000 / 60 / 60;

2

这个解决方案适用于计算两个不同的军事时间之间的差异。

示例格式:开始时间 = 23:00 / 结束时间 = 02:30

function diff(start, end) {
start = start.split(":");
end = end.split(":");
if(Number(start[0]) > Number(end[0]) ) {
  var num = Number(start[0])
  var countTo = Number(end[0]);
  var count = 0;
  for (var i = 1; num != countTo;) {
    num = num + i
    if(num > 24) {
      num = 0
    }
    count++
  }
  var hours = count - 1;
  var startDate = new Date(0, 0, 0, start[0], start[1], 0);
  var endDate = new Date(0, 0, 0, end[0], end[1], 0);
  if(startDate.getMinutes() > endDate.getMinutes()) {
    var hours = count - 2;
    var diff = 60 - (startDate.getMinutes() - endDate.getMinutes());      
  } else {
    var diff = endDate.getMinutes() - startDate.getMinutes();      
  }
  var minutes = diff
} else {
  var startDate = new Date(0, 0, 0, start[0], start[1], 0);
  var endDate = new Date(0, 0, 0, end[0], end[1], 0);
  var diff = endDate.getTime() - startDate.getTime();
  var hours = Math.floor(diff / 1000 / 60 / 60);
  diff -= hours * 1000 * 60 * 60;
  var minutes = Math.floor(diff / 1000 / 60);
}

var returnValue = (hours < 9 ? "0" : "") + hours + ":" + (minutes < 9 ? "0" : "") + minutes

return returnValue;

}


它不能在开始时间为5:30,结束时间为5:00时工作。返回0-1:30。 - Deepesh Thapa

1
这是已经提交过的版本的更新版。它是第二个版本。
function diff(start, end) {
    start = start.split(":");
    end = end.split(":");
    var startDate = new Date(0, 0, 0, start[0], start[1], 0);
    var endDate = new Date(0, 0, 0, end[0], end[1], 0);
    var diff = endDate.getTime() - startDate.getTime();
    var hours = Math.floor(diff / 1000 / 60 / 60);
    diff -= hours * (1000 * 60 * 60);
    var minutes = Math.floor(diff / 1000 / 60);
    diff -= minutes * (1000 * 60);
    var seconds = Math.floor(diff / 1000);

    // If using time pickers with 24 hours format, add the below line get exact hours
    if (hours < 0)
       hours = hours + 24;

    return (hours <= 9 ? "0" : "") + hours + ":" + (minutes <= 9 ? "0" : "") + minutes + (seconds<= 9 ? "0" : "") + seconds;
}

我的更新版本:

可以将日期转换为毫秒,并以此为基础进行计算,而不是拆分日期。

例子:年/月/周/日/小时/分钟/秒

示例:https://jsfiddle.net/jff7ncyk/308/


1

您提供的秒数未能给我带来结果,请使用此处提供的更新函数,它将为您提供正确的秒数 - 作者:Dinesh J

function diff(start, end) {
    start = start.split(":");
    end = end.split(":");
    var startDate = new Date(0, 0, 0, start[0], start[1],start[2], 0);
    var endDate = new Date(0, 0, 0, end[0], end[1],end[2], 0);
    var diff = endDate.getTime() - startDate.getTime();
    var hours = Math.floor(diff / 1000 / 60 / 60);
    diff -= hours * 1000 * 60 * 60;
    var minutes = Math.floor(diff / 1000 / 60);
    var seconds = Math.floor(diff / 1000)-120;

    // If using time pickers with 24 hours format, add the below line get exact hours
    if (hours < 0)
        hours = hours + 24;

    return (hours <= 9 ? "0" : "") + hours + ":" + (minutes <= 9 ? "0" : "") + minutes+ ":" + (seconds <= 9 ? "0" : "") + seconds;
}

1
这个工作几乎很好。现在使用这段代码来计算:23:50 - 00:10,看看你得到了什么。或者是23:30 - 01:30。那是一团糟。 因为在php中用另一种方式得到答案是:
$date1 = strtotime($_POST['started']);
$date2 = strtotime($_POST['ended']);
$interval = $date2 - $date1;
$playedtime = $interval / 60;

但它仍然像你的一样工作。我猜也需要加入日期?

再次强调:我的艰苦研究和开发帮助了我。

if (isset($_POST['calculate'])) {
$d1 = $_POST['started'];
$d2 = $_POST['ended'];
if ($d2 < $d1) {
    $date22 = date('Y-m-');
    $date222 = date('d')-1;
    $date2 = $date22."".$date222;
} else {
$date2 = date('Y-m-d');
}
$date1 = date('Y-m-d');
$start_time = strtotime($date2.' '.$d1);
$end_time = strtotime($date1.' '.$d2); // or use date('Y-m-d H:i:s') for current time
$playedtime = round(abs($start_time - $end_time) / 60,2);
}

这就是如何计算到第二天的时间。 //编辑。我最初把date1和date2搞反了。我需要减去1,因为这个计算只适用于下一天,而第一个日期是昨天。

经过改进和与我的朋友集思广益后,我们得出了以下结论:

$begin=mktime(substr($_GET["start"], 0,2),substr($_GET["start"], 2,2),0,1,2,2003);
$end=mktime(substr($_GET["end"], 0,2),substr($_GET["end"], 2,2),0,1,3,2003);
$outcome=($end-$begin)-date("Z");
$minutes=date("i",$outcome)+date("H",$outcome)*60; //Echo minutes only
$hours = date("H:i", $outcome); //Echo time in hours + minutes like 01:10 or something.

所以,实际上您只需要4行代码即可获得结果。您可以仅花费几分钟或显示完整时间(例如差异为02:32)2小时32分钟。最重要的是:您仍然可以在24小时时钟中计算过夜,例如:开始时间为晚上11:50到01:00 AM(在24小时制中为23:50-01:00),因为在12小时模式下它仍然有效。
最重要的是:您不必格式化输入。您可以使用纯粹的2300作为23:00输入。此脚本将自动将文本字段输入转换为正确的格式。最后一个脚本使用带有method="get"的标准html表单,但您也可以将其转换为使用POST方法。

0
   calTimeDifference(){
    this.start = dailyattendance.InTime.split(":");
    this.end = dailyattendance.OutTime.split(":");
    var time1 = ((parseInt(this.start[0]) * 60) + parseInt(this.start[1]))
    var time2 = ((parseInt(this.end[0]) * 60) + parseInt(this.end[1]));
    var time3 = ((time2 - time1) / 60);
    var timeHr = parseInt(""+time3);
    var  timeMin = ((time2 - time1) % 60);
}

0

TimeCount = function()
{
    t++;
    var ms = t;
    if (ms == 99)
    {
        s++;
        t = 0;
        if ( s == 60)
        {
            m++;
            s = 0;
        }
    }

    Dis_ms = checkTime(ms);
    Dis_s = checkTime(s);
    Dis_m = checkTime(m);
    
    document.getElementById("time_val").innerHTML = Dis_m + ":" + Dis_s+ ":" + Dis_ms;
}

function checkTime(i) 
{
  if (i<10) {
    i = "0" + i;
  }
  return i;
} 


这段代码不完整 - 这是一个答案还是一个提问的尝试? - barbsan

0

根据您允许输入的内容,这个程序可以工作。如果您想允许1am到1pm之间的时间,可能会有一些边界问题。

注意:此程序不使用日期对象或moment.js库。

function pad(num) {
  return ("0"+num).slice(-2);
}
function diffTime(start,end) {
  var s = start.split(":"), sMin = +s[1] + s[0]*60,
      e =   end.split(":"), eMin = +e[1] + e[0]*60,
   diff = eMin-sMin;
  if (diff<0) { sMin-=12*60;  diff = eMin-sMin }
  var h = Math.floor(diff / 60),
      m = diff % 60;
  return "" + pad(h) + ":" + pad(m);
}  
document.getElementById('button').onclick=function() {
  document.getElementById('delay').value=diffTime(
      document.getElementById('timeOfCall').value, 
      document.getElementById('timeOfResponse').value
  );
}
<input type="time" id="timeOfCall">
<input type="time" id="timeOfResponse">
<button type="button" id="button">CLICK</button>
<input type="time" id="delay">


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接