計算N天前的日期

/**
功能:計算N天前的日期
@params days:天數
@return N天前的日期,"yyyy-MM-dd" 格式
**/
function calDate(days) {
    var now = new Date();
    if (days >= 1) {
        now = new Date(now.getTime() - 86400000 * days); //以毫秒計,1000ms*60s*1H*24H * N天
    }
    var yyyy = now.getFullYear(), mm = (now.getMonth() + 1).toString(), dd = now.getDate().toString();
    if (mm.length == 1) {
        mm = "0" + mm;
    }
    if (dd.length == 1) {
        dd = "0" + dd;
    }
    return (yyyy + "-" + mm + "-" + dd);
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章