获取前一个月或后一个月的每一天、周几、单个日期(不填充0)

2020-12-09  本文已影响0人  偶头像超凶

在原js上进一步处理(转载于:https://www.cnblogs.com/wn798/p/12016744.html

export default {
    data() {
        return {
            dateList: [],//日期
        };
    },
    methods: {
        getTime() {
            let startDate = new Date();
            let endDate = new Date();
            // endDate.setDate(startDate.getDate() + 30); //获取后一个月
            startDate.setDate(endDate.getDate() - 30); //获取前一个月
            while (endDate.getTime() - startDate.getTime() >= 0) {
                let year = startDate.getFullYear();
                let month = (startDate.getMonth() + 1).toString().length === 1 ? '0' + (startDate.getMonth() + 1).toString() : startDate.getMonth() + 1;
                let day = startDate.getDate().toString().length === 1 ? '0' + startDate.getDate() : startDate.getDate();
                let onlyDay = startDate.getDate(); //日期 不填充0
                let week = '' //周几
                if(startDate.getDay() == 0){
                    week = '日';   
                }else if(startDate.getDay() == 1){
                    week = '一';
                }else if(startDate.getDay() == 2){
                    week = '二';
                }else if(startDate.getDay() == 3){
                    week = '三';
                }else if(startDate.getDay() == 4){
                    week = '四';
                }else if(startDate.getDay() == 5){
                    week = '五';
                }else if(startDate.getDay() == 6){
                    week = '六';
                }
                this.dateList.push({week: week, time: year +'-'+ month + '-' + day, onlyDay: onlyDay});
                startDate.setDate(startDate.getDate() + 1);
            }
            console.log(this.dateList);
        }
    },
    onLoad() {
        this.getTime();
    }
};
上一篇下一篇

猜你喜欢

热点阅读