Get week of the month

前端 未结 14 1662
野趣味
野趣味 2020-11-30 08:55

How can i get the week number of month using javascript / jquery?

For ex.:

First Week: 5th July, 2010. / Week Number = First monday

相关标签:
14条回答
  • 2020-11-30 09:10

    function weekAndDay(date) {
        
        var days = ['Sunday','Monday','Tuesday','Wednesday',
                    'Thursday','Friday','Saturday'],
            prefixes = ['First', 'Second', 'Third', 'Fourth', 'Fifth'];
    
        return prefixes[Math.floor(date.getDate() / 7)] + ' ' + days[date.getDay()];
    
    }
    
    console.log( weekAndDay(new Date(2010,7-1, 5)) ); // => "First Monday"
    console.log( weekAndDay(new Date(2010,7-1,12)) ); // => "Second Monday"
    console.log( weekAndDay(new Date(2010,7-1,19)) ); // => "Third Monday"
    console.log( weekAndDay(new Date(2010,7-1,26)) ); // => "Fourth Monday"
    console.log( weekAndDay(new Date()) );

    Adding the capability to have Last ... may take some more hacking...

    0 讨论(0)
  • 2020-11-30 09:11

    After reading all the answers I figured out a way that use less CPU than the others and work for every day of every month of every year. Here is my code:

    function getWeekInMonth(year, month, day){
    
        let weekNum = 1; // we start at week 1
    
        let weekDay = new Date(year, month - 1, 1).getDay(); // we get the weekDay of day 1
        weekDay = weekDay === 0 ? 6 : weekDay-1; // we recalculate the weekDay (Mon:0, Tue:1, Wed:2, Thu:3, Fri:4, Sat:5, Sun:6)
    
        let monday = 1+(7-weekDay); // we get the first monday of the month
    
        while(monday <= day) { //we calculate in wich week is our day
            weekNum++;
            monday += 7;
        }
    
        return weekNum; //we return it
    }
    

    I hope this can help.

    0 讨论(0)
  • 2020-11-30 09:19

    I think this works. It returns the week of the month, starting at 0:

    var d = new Date();
    var date = d.getDate();
    var day = d.getDay();
    
    var weekOfMonth = Math.ceil((date - 1 - day) / 7);
    
    0 讨论(0)
  • 2020-11-30 09:19

    I was just able to figure out a easier code for calculate the number of weeks for a given month of an year ..

    y == year for example { 2012 } m == is a value from { 0 - 11 }

    function weeks_Of_Month( y, m ) {
        var first = new Date(y, m,1).getDay();      
        var last = 32 - new Date(y, m, 32).getDate(); 
    
        // logic to calculate number of weeks for the current month
        return Math.ceil( (first + last)/7 );   
    }
    
    0 讨论(0)
  • 2020-11-30 09:24

    This is nothing natively supported.

    You could roll your own function for this, working from the first day of the month

    var currentDate = new Date();
    var firstDayOfMonth = new Date( currentDate.getFullYear(), currentDate.getMonth(), 1 );
    

    And then getting the weekday of that date:

    var firstWeekday = firstDayOfMonth.getDay();
    

    ... which will give you a zero-based index, from 0-6, where 0 is Sunday.

    0 讨论(0)
  • 2020-11-30 09:24
    function weekNumberForDate(date){
    
        var janOne = new Date(date.getFullYear(),0,1);
        var _date = new Date(date.getFullYear(),date.getMonth(),date.getDate());
        var yearDay = ((_date - janOne + 1) / 86400000);//60 * 60 * 24 * 1000
        var day = janOne.getUTCDay();
        if (day<4){yearDay+=day;}
        var week = Math.ceil(yearDay/7);
    
       return week;
    }
    

    Apparently the first week of the year is the week that contains that year's first Thursday.

    Without calculating the UTCDay, the returned week was one week shy of what it should have been. Not confident this can't be improved, but seems to work for now.

    0 讨论(0)
提交回复
热议问题