javascript - elapsed minutes between two times

限于喜欢 提交于 2019-12-06 16:53:54
var start_time ="830";
var end_time ="1930";

var start_hour = start_time.slice(0, -2);
var start_minutes = start_time.slice(-2);

var end_hour = end_time.slice(0, -2);
var end_minutes = end_time.slice(-2);

var startDate = new Date(0,0,0,start_hour, start_minutes);
var endDate = new Date(0,0,0,end_hour, end_minutes);

var millis = endDate - startDate;
var minutes = millis/1000/60;
alert(minutes);

--output:--
660

DEMO

Just remove what you don't want to see from the return. Now you can simply pass date objects in, you don't have to bother with conversions.

var cc = new Date("9/13/2012 12:00 AM");
var c = new Date();

function timeDifference(d, dd) {
    var minute = 60 * 1000,
        hour = minute * 60,
        day = hour * 24,
        month = day * 30,
        ms = Math.abs(d - dd);

    var months = parseInt(ms / month, 10);

        ms -= months * month;

    var days = parseInt(ms / day, 10);

        ms -= days * day;

    var hours = parseInt(ms / hour, 10);

        ms -= hours * hour;
    var minutes = parseInt(ms / minute, 10);

    return [
        months + " months",
        days + " days",
        hours + " hours",
        minutes + " minutes"
    ].join(", ");
};

document.body.innerHTML += timeDifference(cc, c) + "<br />";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!