1. 程式人生 > >js 計算日期

js 計算日期

//獲得多少天之前的日期 列如2018-01-01
function getDay(day){
    var today = new Date();
    var targetday_milliseconds=today.getTime() + 1000*60*60*24*day;
    today.setTime(targetday_milliseconds); //注意,這行是關鍵程式碼
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = tMonth + 1;
    tDate = tDate;
    return tYear+"-"+tMonth+"-"+tDate;
}
//獲得當前日期
function getNowDay(){
    var today = new Date();
    var tYear = today.getFullYear();
    var tMonth = today.getMonth();
    var tDate = today.getDate();
    tMonth = tMonth + 1;
    tDate = tDate;
    return tYear+"-"+tMonth+"-"+tDate;
}