admin管理员组

文章数量:1278886

How i can get the date of the first day of a week from the week number and the year, in javascript (or jquery)?

example : week 30 , year 2013

thanks in advance !

How i can get the date of the first day of a week from the week number and the year, in javascript (or jquery)?

example : week 30 , year 2013

thanks in advance !

Share Improve this question asked Jul 25, 2013 at 10:11 ThomasThomas 1,1403 gold badges18 silver badges37 bronze badges 5
  • 1 Possible duplicate of link – Chirag Vidani Commented Jul 25, 2013 at 10:17
  • You need to calculate that using Javascripts Date function. – putvande Commented Jul 25, 2013 at 10:17
  • Duplicate to this too stackoverflow./questions/7580824/… – Chirag Vidani Commented Jul 25, 2013 at 10:18
  • yes you're right chirag, it's a duplicated question. sorry :x – Thomas Commented Jul 25, 2013 at 12:38
  • Possible duplicate of javascript calculate date from week number – Oleg Mikhailov Commented Jun 15, 2016 at 9:34
Add a ment  | 

2 Answers 2

Reset to default 9
var d = new Date(year, 0, 1);
var days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];

d.setDate(d.getDate() + (week * 7));

console.log(days[d.getDay()]);

Try This

Using ISO 8601 week numbering, you can start with the first week of the year and add (n - 1) * 7 days to get the first day of the required week. E.g.

function getDateByWeek(week, year) {

  // Create a date for 1 Jan in required year
  var d = new Date(year, 0);
  // Get day of week number, sun = 0, mon = 1, etc.
  var dayNum = d.getDay();
  // Get days to add
  var requiredDate = --week * 7;

  // For ISO week numbering
  // If 1 Jan is Friday to Sunday, go to next week 
  if (dayNum != 0 || dayNum > 4) {
    requiredDate += 7;
  }

  // Add required number of days
  d.setDate(1 - d.getDay() + ++requiredDate);
  return d;
}

// Week 44 of 2021 starts on Mon 1 Nov 2021
console.log(getDateByWeek(44,2021).toString());

If some other week numbering scheme is required, just adjust the starting date accordingly.

本文标签: jqueryhow to get a javascript date from a week numberStack Overflow