獲取當前日期往前推三個月(當前日期減去三個月)

/**
 * 獲取當前日期往前推三個月(當前日期減去三個月)
 * 返回日期格式:比如:2014-08-11(當前月為:2014-11-11)
 */
function getStartDate() {
    var newDate = new Date();
    var startY = newDate.getFullYear();
    var startM = newDate.getMonth() - 3;
    var startD = newDate.getDate();
    // 日期賦值
    var mydate = new Date(startY, startM, startD);
    var strYear = mydate.getFullYear();
    //顯示的月份要加一,因為月份是從0開始的
    var strMonth = mydate.getMonth() + 1;
    var strDate = mydate.getDate();
    if (strMonth.toString().length == 1)
        strMonth = "0" + strMonth;
    if (strDate.toString().length == 1)
        strDate = "0" + strDate;
    var strStartDate = strYear + "-" + strMonth + "-" + strDate;
    return strStartDate;
};

你可能感兴趣的:(js)