Get the first few weeks of the current year for the specified date

    function getWeek(dt){
        //参数dt的格式为2018-12-12
        var y, m, d;
        y = dt.split('-')[0];
        m = dt.split('-')[1];
        d = dt.split('-')[2];
        var now = new Date(y, m - 1, d),
          year = now.getFullYear(),
          month = now.getMonth(),
          days = now.getDate();
        //那一天是那一年中的第多少天
        for (var i = 0; i < month; i++) {
          days += getMonthDays(year, i);
        }

        //那一年第一天是星期几
        var yearFirstDay = new Date(year, 0, 1).getDay() || 7;
        
        var week = null;
        if (yearFirstDay == 1) {
          week = Math.ceil(days / yearFirstDay);
        } else {
          days -= (7 - yearFirstDay + 1);
          week = Math.ceil(days / 7) + 1;
        }

        return week;
    }

 

Guess you like

Origin blog.csdn.net/qq_37514029/article/details/85678820