JS时间从日期开始经过

时间:2014-05-22 15:34:09

标签: javascript date

我想计算两个日期之间的经过时间。我在互联网上看到了一些例子(大部分都在这个网站上),但没有发现任何有用的东西。我想写一个函数可以像这样调用:

calculateDifference('2012-02-01 15:31')

没有第二个参数,因为它是当前日期。我有一个我正在使用的代码,即:

    function get_time_diff(_datetime )
    {
      var datetime = new Date( _datetime ).getTime();
      var now = new Date().getTime();

      if( isNaN(datetime) )
      {
        return " on " + _datetime;
      }


      if (datetime < now) {
        var milisec_diff = now - datetime;
      }else{
        var milisec_diff = datetime - now;
      }

      var days = Math.floor(milisec_diff / 1000 / 60 / (60 * 24));

      var date_diff = new Date( milisec_diff );
      var respvalue ='';

      if (days > 0) {
        respvalue += days + " day(s), ";
      }

      if (date_diff.getHours() > 0) {
        respvalue += (date_diff.getHours() - 1) + " hour(s) and ";
      }

      respvalue += date_diff.getMinutes() + " minute(s) ago.";
      return respvalue;
    }

结果是并且应该是:

1 day(s), 14 hour(s) and 17 minute(s)

由于某些原因存在差异(当1天过去时显示0等)并且它仅适用于chrome,在IE和FF中它返回我作为参数传递的日期。 再一次:我想用下一种格式计算当前日期和给定日期之间的差异:

1 day(s), 14 hour(s) and 17 minute(s)

我不关心几个月,几年。只有几小时,几分钟和几秒钟。提前谢谢!

3 个答案:

答案 0 :(得分:1)

试试这个:

  function get_time_diff(_datetime )
  {
    var datetime = new Date( _datetime ).getTime();
    var now = new Date().getTime();

    if( isNaN(datetime) )
    {
      return " on " + _datetime;
    }


    if (datetime < now) {
      var milisec_diff = now - datetime;
    }else{
      var milisec_diff = datetime - now;
    }

    var days = Math.floor(milisec_diff / 1000 / 60 / (60 * 24));
    var hours = Math.floor(milisec_diff / (1000 * 60 * 60) - days * 24);
    var minutes = Math.floor(milisec_diff / (1000 * 60) - days * 24 * 60 - hours * (60));

    var respvalue ='';

    if (days > 0) {
      respvalue += days + " day(s), ";
    }

    if (hours > 0) {
      respvalue += hours + " hour(s) and ";
    }

    respvalue += minutes + " minute(s) ago.";

    return respvalue;
  }

答案 1 :(得分:1)

问题是您的日期格式无效,因此Firefox无法将您的字符串解析为日期。

您可以使用

function get_time_diff(datetime) {
      var milisec_diff = Math.abs(new Date() - new Date(datetime)),
          diff = new Date(milisec_diff),
          days = milisec_diff / 3600e3 / 24 | 0,
          hours = diff.getUTCHours(),
          respvalue = '';
      if (days)
          respvalue += days + " day(s), ";
      if (hours)
          respvalue += hours + " hour(s) and ";
      respvalue += diff.getUTCMinutes() + " minute(s) ago.";
      return respvalue;
}
get_time_diff('2012-02-01T15:31Z');

日期2012-02-01T15:31Z位于ISO8601Z表示UTC时间(请注意某些浏览器可能不支持)。

答案 2 :(得分:1)

这对我来说似乎最简单:

http://jsbin.com/tusul/9/edit

// end date is optional, it will assume the current date if not supplied.
// if timezone is not supplied, it will assume local browser time.
function calculateDateDiff(beginDate, endDate) {
  var currentDate;
  if (typeof(endDate) == 'undefined') {
    currentDate = new Date();
  } else {
    currentDate = new Date(endDate);
  }
  var targetDate = new Date(beginDate);

  var differenceDate;
  if (currentDate > targetDate) {
    differenceDate = new Date(currentDate - targetDate); 
  } else {
    differenceDate = new Date(targetDate - currentDate);
  }

  return('Days: ' + (differenceDate.getUTCDate() -1) + ', Hours: ' + differenceDate.getUTCHours() + ', Minutes: ' + differenceDate.getUTCMinutes() + ', Seconds: ' + differenceDate.getUTCSeconds());
}

console.log(calculateDateDiff('05-22-2014 01:02:03', '05-22-2014 02:03:04'));
console.log(calculateDateDiff('05-22-2014 01:02:03', '05-22-2014 02:03:04-600'));
console.log(calculateDateDiff('05-22-2014 01:02:03Z', '05-22-2014 02:03:04Z'));
console.log(calculateDateDiff('05-22-2014 01:02:03Z', '05-22-2014 02:03:04-600'));
console.log(calculateDateDiff('05-22-2014 01:02:03-500', '05-22-2014 02:03:04-600'));
console.log(calculateDateDiff('05-22-2014 01:02:03+1000', '05-22-2014 02:03:04-600'));