美文网首页
js 获取一年月份

js 获取一年月份

作者: 多点干货少点废话 | 来源:发表于2019-08-07 15:06 被阅读0次
    
    function  mathDate(){
        var nowDate = new Date()
        let nowYear = nowDate.getFullYear();
        let nowMonth = (nowDate.getMonth())<10?'0'+(nowDate.getMonth()):(nowDate.getMonth());
        var d = new Date(nowYear+'-'+nowMonth);
        var result = [];
        d.setMonth(d.getMonth() + 2);
        for(var i = 1; i < 13; i++) {
            d.setMonth(d.getMonth() - 1);
            let m = d.getMonth() + 1;
            m = m < 10 ? ("0" + m) : m;
            let val = {
                value: d.getFullYear() + "年" + m + '月',
                label:d.getFullYear() + "-" + m
            }
            //在这里可以自定义输出的日期格式
            result.push(val);
        }
        // 一年的日期
            
        // 当前年月
        let thisMonth = (d.getMonth())<10?'0'+(d.getMonth()):(d.getMonth());
        let thisYear = d.getFullYear()+1;
        if(thisMonth == 0){
            this.nowData = (thisYear-1)+"-12";
        }else{
            this.nowData = thisYear+"-"+thisMonth;
        }
        let nowobj ={
            value:thisYear+'年'+thisMonth+'月',
            label:thisYear+'-'+thisMonth
        }
        this.selectData = result;
        this.selectValue = this.nowData
    }
    
    

    可以添加当前月份 把获取的数组处理一下

    相关文章

      网友评论

          本文标题:js 获取一年月份

          本文链接:https://www.haomeiwen.com/subject/xaiodctx.html