使用时间戳查找年月日时分钟

时间:2014-06-20 09:52:42

标签: javascript mysql timestamp

我正在使用mysql时间戳来计算通过JavaScript的时间差异,如下所示

function parseMySQLTimestamp(timestamp) {
    var parts = timestamp.match(/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/);
    return new Date(+parts[1], (+parts[2] - 1), +parts[3], +parts[4], +parts[5], +parts[6]);
}




var informTime = "2011-11-09 08:00:00";
  var diff = (new Date()- parseMySQLTimestamp(informTime));

  var months = Math.floor(diff /(60*60*24*30*1000));
  diff=Math.abs(diff-(months*60*60*24*30*1000)); 
  var days = Math.floor(diff/(60 * 60 * 24*1000));
  diff=Math.abs(diff-(days*60*60*24*1000)); 
  var hour=Math.floor(diff/(60*60*1000));
  diff=Math.abs(diff-(hour*60*60*1000)); 
  var minute=Math.floor(diff/(60*1000));

  var message=months +" months "+ days + " D " + hour + " Hr " + minute + "min ago ";
  alert(message);

它会给出正确的结果,但我担心在闰年有31,28或29天的月份它会不正确。

如何实现可靠的功能?我觉得它太复杂了。谢谢。

0 个答案:

没有答案