最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

date - Get friday from week number and year in javascript - Stack Overflow

programmeradmin2浏览0评论

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?

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?

Share Improve this question edited Dec 29, 2010 at 15:36 Peter asked Dec 29, 2010 at 15:23 PeterPeter 9141 gold badge15 silver badges27 bronze badges 5
  • Surely getDay(day_num, week_num, year) would be more useful? – Jim Commented Dec 29, 2010 at 15:27
  • "You need to convert that to Friday of that particular week"? - Please reword your question - it is hard to tell what you are asking for exactly. – JM4 Commented Dec 29, 2010 at 15:30
  • @Jim day_num is always 5 (friday), your function will return same result, yes – Peter Commented Dec 29, 2010 at 15:31
  • 1 @JM4 He means that if we call getFriday(2,2011) it will return a date object of the 2nd Friday in 2011 (14th Jan 2011) – Jim Commented Dec 29, 2010 at 15:33
  • @JM4 reworded my question a bit – Peter Commented Dec 29, 2010 at 15:42
Add a comment  | 

4 Answers 4

Reset to default 17

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

Use the date.js library. It's great for all date-related functions.

Here's some quick code

var DAY = 86400000;

function getFriday(weekNum, year) {
  var year = new Date(year.toString()); // toString first so it parses correctly year numbers
  var daysToFriday = (5 - year.getDay()); // Note that this can be also negative
  var fridayOfFirstWeek = new Date(year.getTime() + daysToFriday * DAY);
  var nthFriday = new Date(fridayOfFirstWeek.getTime() + (7 * (weekNum - 1) * DAY));
  return nthFriday;
}

Split some variables for readability.

But if you find yourself writing more complex time operations, you're better using a library instead.

i think if i should not use any date library i would:

assuming u use a christian week, where sunday is the frist day of the week. this is neccessary to find out if the first days in a year belong to the first week of the year or not.

create an array. months = new Array(31,28,31, ... ) if(year % 4 == 0) then february has 29 days.

days = num_week * 7;

then iterate over the month and decrease days by month[current]. if days gets negative increase days with the current month days again.

result: year-(current+1)-days

i hope this helps you. what u have to add on your own is the handling for january and the first days.

发布评论

评论列表(0)

  1. 暂无评论