JS获取本月第一天和最后一天(月末和月初)

var nowDate = new Date();
var cloneNowDate = new Date();
var fullYear = nowDate.getFullYear();
var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11,代表1-12月
var endOfMonth = new Date(fullYear, month, 0).getDate(); // 获取本月最后一天
function getFullDate(targetDate) {
        var D, y, m, d;
        if (targetDate) {
            D = new Date(targetDate);
            y = D.getFullYear();
            m = D.getMonth() + 1;
            d = D.getDate();
        } else {
            y = fullYear;
            m = month;
            d = date;
        }
        m = m > 9 ? m : '0' + m;
        d = d > 9 ? d : '0' + d;
        return y + '-' + m + '-' + d;
    };
var endDate = getFullDate(cloneNowDate.setDate(endOfMonth));//当月最后一天
var starDate = getFullDate(cloneNowDate.setDate(1));//当月第一天

posted on 2019-06-20 09:26  ran_D  阅读(4417)  评论(0编辑  收藏  举报

导航