Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - How does JS calculate the dates of Monday and Sunday in a week by year?
How does JS calculate the dates of Monday and Sunday in a week by year?
The following algorithm is 1 week in 2009. Starting from the first Monday, it is 1.5. Otherwise, Monday of 2009 1 week will be transferred to 20081February.

& lt script & gt

Fun (2008, 30); //30th week of 2008

Function fun (year, week) {

Var d = new date (year, 0,1);

var time = d . gettime();

Var t = week * 7 * 24 * 3600 *1000;

Time+= t;

D = new date (time);

var w = d . getday();

Var d 1 = new date (time-(w-1) * 24 * 3600 *1000);

Var d2 = new date (time+((7-w) * 24 * 3600 *1000));

Print (d1);

Printing (d2);

}

Function print (d)

var y = d . get full year();

var m = d . get month()+ 1;

m = m & lt 10 ? “0”+m:“+m;

var day = d . getdate();

day = day & lt 10 ? "0"+days: "+days;

var weekDay = d . getday();

Var wk = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

alert(y+"-"+m+"-"+day+" "+wk[weekDay]);

}

& lt/script & gt;