找到24小时格式的时差

时间:2014-06-05 11:42:04

标签: javascript

如何获得24小时格式的两个值的时差?

例如

var time1 = 22:30:00,
    time2 = 06:30:00;

差异应该是08:00:00

3 个答案:

答案 0 :(得分:1)

使用完整日期对象进行此类数学操作要好得多,否则您必须对时间值进行猜测,例如,如果完成时间小于开始时间,则必须在第二天进行。

以下内容包括一些辅助函数和一个主要功能,以便发挥作用。

// Convert h:m:s to seconds
function hmsToSecs(s) {
  var b = s.split(':');
  return b[0]*3.6e3 + b[1]*60 + +b[2];
}

// Convert seconds to hh:mm:ss
function secsToHMS(n) {
  function z(n){return (n<10? '0':'') + n;}
  var sign = n < 0? '-' : '';
  n = Math.abs(n);
  return sign + z(n/3.6e3|0) + ':' + z(n%3.6e3/60|0) + ':' + z(n%60);
}

// Calculate time difference between two times
// start and finish in hh:mm:ss
// If finish is less than start, assume it's the following day
function timeDiff(start, finish) {
  var s = hmsToSecs(start);
  var f = hmsToSecs(finish);

  // If finish is less than start, assume is next day
  // so add 24hr worth of seconds
  if (f < s) f += 8.64e4;

  return secsToHMS(f - s);
}

console.log(timeDiff('22:30:00','06:30:00')); //  08:00:00
console.log(timeDiff('06:30:00','22:30:00')); //  16:00:00

使用完整日期对象,您可以:

var start = new Date(2014,5,5,22,30);  // 22:30:00 on 5 June 2014
var finish = new Date(2014,5,6,6,30);  // 06:30:00 on 6 June 2014

// Subtract dates to get difference in ms, convert to seconds and format
console.log(secsToHMS((finish - start)/1000)); //  08:00:00
console.log(secsToHMS((start - finish)/1000)); // -08:00:00

答案 1 :(得分:0)

我建议你应该使用jquery date.js库,然后你可以使用它的Timespan类,如下所示:

var future = Date.parseExact("22:30:00", "hh:mm:ss"); 

var past = Date.parseExact("06:30:00", "hh:mm:ss"); 

var span = new TimeSpan(future - now); 

你的小时数差异如下:

span.getHours() + ":" span.getMinutes() + ":" span.getSeconds()

答案 2 :(得分:-2)

如果你想在C#中使用Diff函数;

DateTime oldDate= "06/01/2014 12:00:00 AM";
TimeSpan timeDiff = DateTime.Now - oldDate;

int diff =Convert.ToInt32(timeDiff.TotalHours);

如果你想在JavaScript中使用脚本块可以帮助你;

  function diffDateTime(startDT, endDT) {

        if (typeof startDT == 'string' && startDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}[amp ]{0,3}$/i)) {
            startDT = startDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/);
            startDT = startDT.toString().split(':');
            var obstartDT = new Date();
            obstartDT.setHours(startDT[0]);
            obstartDT.setMinutes(startDT[1]);
            obstartDT.setSeconds(startDT[2]);
        }
        else if (typeof startDT == 'string' && startDT.match(/^now$/i)) var obstartDT = new Date();
        else if (typeof startDT == 'string' && startDT.match(/^tomorrow$/i)) {
            var obstartDT = new Date();
            obstartDT.setHours(24);
            obstartDT.setMinutes(0);
            obstartDT.setSeconds(1);
        }
        else var obstartDT = new Date(startDT);

        if (typeof endDT == 'string' && endDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}[amp ]{0,3}$/i)) {
            endDT = endDT.match(/^[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}/);
            endDT = endDT.toString().split(':');
            var obendDT = new Date();
            obendDT.setHours(endDT[0]);
            obendDT.setMinutes(endDT[1]);
            obendDT.setSeconds(endDT[2]);
        }
        else if (typeof endDT == 'string' && endDT.match(/^now$/i)) var obendDT = new Date();
        else if (typeof endDT == 'string' && endDT.match(/^tomorrow$/i)) {
            var obendDT = new Date();
            obendDT.setHours(24);
            obendDT.setMinutes(0);
            obendDT.setSeconds(1);
        }
        else var obendDT = new Date(endDT);
        var secondsDiff = (obendDT.getTime() - obstartDT.getTime()) > 0 ? (obendDT.getTime() - obstartDT.getTime()) / 1000 : (86400000 + obendDT.getTime() - obstartDT.getTime()) / 1000;
        secondsDiff = Math.abs(Math.floor(secondsDiff));

        var oDiff = {};     // object that will store data returned by this function

        oDiff.days = Math.floor(secondsDiff / 86400);
        oDiff.totalhours = Math.floor(secondsDiff / 3600);      // total number of hours in difference
        oDiff.totalmin = Math.floor(secondsDiff / 60);      // total number of minutes in difference
        oDiff.totalsec = secondsDiff;      // total number of seconds in difference

        secondsDiff -= oDiff.days * 86400;
        oDiff.hours = Math.floor(secondsDiff / 3600);     // number of hours after days

        secondsDiff -= oDiff.hours * 3600;
        oDiff.minutes = Math.floor(secondsDiff / 60);     // number of minutes after hours

        secondsDiff -= oDiff.minutes * 60;
        oDiff.seconds = Math.floor(secondsDiff);     // number of seconds after minutes

        return oDiff;
    }

使用;

 var objDiff = diffDateTime('06/01/2014 12:00:00 AM', 'now');
 var dtdiff = objDiff.days + ' days, ' + objDiff.hours + ' hours, ' + objDiff.minutes + ' minutes, ' + objDiff.seconds + ' seconds';

重要事项:您必须记住DateTime格式必须采用en-US格式dd / MM / yyyy hh:mm:ss。