计算两个日期时间角之间的时间

时间:2017-04-13 13:10:43

标签: angularjs datetime ionic-framework

我有两个由服务器发送的日期时间,我想计算持续时间差异和IONIC App。

我试图这样做,但我没有成功。

"created_date": "2017-04-13 10:12:12",

"current_time": "2017-04-13 11:10:46"

差异= current_time - created_date

请帮助我。

1 个答案:

答案 0 :(得分:2)

此工作代码将为您提供帮助。



    var inputJSON = {
        "created_date": "2017-04-13 10:12:12",
        "current_time": "2017-04-13 11:10:46"
    };

    function getDataDiff(startDate, endDate) {
        var diff = endDate.getTime() - startDate.getTime();
        var days = Math.floor(diff / (60 * 60 * 24 * 1000));
        var hours = Math.floor(diff / (60 * 60 * 1000)) - (days * 24);
        var minutes = Math.floor(diff / (60 * 1000)) - ((days * 24 * 60) + (hours * 60));
        var seconds = Math.floor(diff / 1000) - ((days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60));
        return { day: days, hour: hours, minute: minutes, second: seconds };
    }
    var diff = getDataDiff(new Date(inputJSON.created_date), new Date(inputJSON.current_time));
    console.log(diff);