1. 程式人生 > >js對時間進行加減操作

js對時間進行加減操作

//時間加減
gu.CountTime=function (t,b) {
    var time = new Date(t.replace("-","/"));
    time.setMinutes(time.getMinutes() + b, time.getSeconds(), 0);
    var d = new Date(time);
    var y = d.getFullYear();
    var m = d.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var da = d.getDate();
    da = da < 10 ? ('0' + da) : da;
    var h = d.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = d.getMinutes();
    minute = minute < 10 ? ('0' + minute) : minute;
    var resDate=y + '-' + m + '-' + da + ' ' + h + ':' + minute;
    return resDate;
}

輸入例子

var time="2018-12-19 09:30";

var b=15;

CountTime(time,b)