Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
171
rated 0 times [  172] [ 1]  / answers: 1 / hits: 20225  / 13 Years ago, tue, january 10, 2012, 12:00:00

In JavaScript I want to get first date of the week and last date of the week by week number and year only.



For example if I my input is:



2(week),2012



Then my output should be:



2012-01-08 and 2012-01-14


More From » jquery

 Answers
46

Try this:





var year = 2012;
var week = 2;
var d = new Date(Jan 01, + year + 01:00:00);
var w = d.getTime() + 604800000 * (week - 1);
var n1 = new Date(w);
var n2 = new Date(w + 518400000)

console.log(n1);
console.log(n2);





n1 contains the first day of the week

n2 contains the last day of the week



As for the constants:

604800000 is one week in milliseconds

518400000 is six days


[#88116] Monday, January 9, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
samara

Total Points: 326
Total Questions: 106
Total Answers: 103

Location: Cook Islands
Member since Thu, May 21, 2020
4 Years ago
;