Get the days in a Week in Javascript, Week starts on Sunday

前端 未结 4 1009
长情又很酷
长情又很酷 2021-01-19 18:29

Here\'s the given input from the user:

Year = 2011, Month = 3 (March), Week = 2

I want to get the days in week 2 of March 2011 in JavaScript.

e.g. Su

4条回答
  •  伪装坚强ぢ
    2021-01-19 18:51

    A little modification of the first answer that worked for me:

    year = 2014;
    week = 31;//week number is 31 for the example, could be 120.. it will just jump trough years
    // get the date for the first day of the year               
    var firstDateOfYear = new Date(year,0,1);
    // set the date to the number of days for the number of weeks
    firstDateOfYear.setDate(firstDateOfYear.getDate()+(7 * (week-1))); 
    // get the number of the day in the week 0 (Sun) to 6 (Sat)
    var counter = firstDateOfYear.getDay();
    
    //make sunday the first day of the week
    for(i=0;i

提交回复
热议问题