差异日期和时间以及返回分钟

时间:2014-07-01 11:02:19

标签: javascript

我有这种格式的日期和时间:2014-07-14 18:19:33。从今天开始我怎么能在几分钟内得到改变? 示例:2014-06-14 18:19:33 - 2014-07-01 13:00:22 = xxxx分钟

function subtractDate(datetime)
{
    var diff = new Date(Math.abs(new Date()-new Date(datetime.replace(/-/g,'/'))));
    //return "0000-" + diff.getMonth() + "-" + diff.getDate() + " " + diff.getHours()+":"+diff.getMinutes()+":"+diff.getSeconds();

    var totalMinutes = 0;

    totalMinutes+=diff.getMinutes()
    totalMinutes+=diff.getHours()*60
    totalMinutes+=diff.getDate()*24*60
    totalMinutes+=diff.getMonth() //problem (29 days or 30 or 31 days)
    totalMinutes+=diff.getFullYear()*365 //problem (365 or 366 days)
    return totalMinutes
}

2 个答案:

答案 0 :(得分:1)

试试这个:

var diff = Math.abs(new Date('2014/06/14 18:19:33').getTime() - new Date('2014/07/01 13:00:22').getTime());
var minutes = Math.floor((diff/1000)/60);

答案 1 :(得分:0)

var d1 = new Date('2014-06-14 18:19:33').getTime();
var d2 = new Date('2014-07-01 13:00:22').getTime();
var d = d1 - d2;
var result = d / 1000 / 60;