Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
655 views
in Technique[技术] by (71.8m points)

date - Get friday from week number and year in javascript

I have week number and year, need to find out date (friday) in that week and year.

function getFriday(week_num, year)
{
    ?

    return friday_date_object;
}

How do I do that?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The week #1 is the week with the first Thursday.

Here is a function to get any day:

var w2date = function(year, wn, dayNb){
    var j10 = new Date( year,0,10,12,0,0),
        j4 = new Date( year,0,4,12,0,0),
        mon1 = j4.getTime() - j10.getDay() * 86400000;
    return new Date(mon1 + ((wn - 1)  * 7  + dayNb) * 86400000);
};
console.log(w2date(2010, 1, 4));

week numbers start at 1 until 52 or 53 it depends the year.
For the day numbers, 0 is Monday, 1 is Tuesday, ... and 4 is Friday


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...