当前位置:   article > 正文

js获取一年中所有的周数和对应的日期_dayjs获取每年的自然周

dayjs获取每年的自然周

js获取一年中所有的周对应的日期
调用: setweekOption(‘2020’)
在这里插入图片描述


//设置周下拉框
export function setweekOption(year) { //传入年份,字符串类型年份,'2020'
    year = new Date(year).getFullYear()
    let days = getDay(year || new Date().getFullYear())
    // console.log(days)
    let weeks = {};
    for (let i = 0; i < days.length; i++) {
        let weeksKeyLen = Object.keys(weeks).length;
        let daySplit = days[i].split('_');
        if (weeks[weeksKeyLen] === undefined) {
            weeks[weeksKeyLen + 1] = [daySplit[0]]
        } else {
            if (daySplit[1] == '1') {
                weeks[weeksKeyLen + 1] = [daySplit[0]]
            } else {
                weeks[weeksKeyLen].push(daySplit[0])
            }
        }
    }
    let option = []
    let weeksKeyLen = Object.keys(weeks).length;
    for (let i = 1; i < weeksKeyLen + 1; i++) {
        let obj = {};
        obj.year = year
        obj.week = "第" + i + "周" 
        obj.md = weeks[i][0] + '-' + weeks[i][weeks[i].length - 1] 
        obj.s = weeks[i][0]
        obj.e = weeks[i][weeks[i].length - 1] 

        obj.text = "第" + i + "周" + '(' + weeks[i][0] + '-' + weeks[i][weeks[i].length - 1] + ')';
        obj.value = i;
        option.push(obj)

    }
    return option;
}
export function getDay(year) {
    let dates = [];
    for (let i = 1; i <= 12; i++) {
        for (let j = 1; j <= new Date(year, i, 0).getDate(); j++) {
            dates.push(formatNumber(i) + '/' + formatNumber(j) + '_' + new Date([year, formatNumber(i), formatNumber(j)].join('-')).getDay()) //返回当年所有日期(带星期数)
        }
    }
    return dates;
}
export function formatNumber(n) {
    return n.toString().length > 1 ? n : '0' + n
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/601871
推荐阅读
相关标签
  

闽ICP备14008679号