JS計算時間差【単位:分】

6483 ワード

//     
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

//     -  
function TimeDifference(time1, time2) {
    //              
    if (time1 > time2) {
        alert("            !");
        return false;
    }

    //     
    var begin1 = time1.substr(0, 10).split("-");
    var end1 = time2.substr(0, 10).split("-");

    //          ,           
    var date1 = new Date(begin1[0] + - + begin1[1] + - + begin1[2]);
    var date2 = new Date(end1[0] + - + end1[1] + - + end1[2]);

    //           m,      
    var m = parseInt(Math.abs(date2 - date1) / 1000 / 60);

    //                
    //time1.substr(11,2)             
    //parseInt(time1.substr(11,2))*60          
    var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
    var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));

    //                ,      
    var n = min2 - min1;

    var sec1 = parseInt(time1.substr(17, 2)) / 60;
    var sec2 = parseInt(time2.substr(17, 2)) / 60;
    var s = sec2 - sec1;

    //                   ,              
    var minutes = m + n+s;
    return minutes.toFixed(2);
}
//  -    
function addMinutes(t, m) {
    var time = new Date(t);
    time.setMinutes(time.getMinutes() + m, time.getSeconds(), 0);
    return time;