2

Given say 2013 and 38 (38 = 38th week of the year) I want to get 2013-09-16. Is there a Javascript or jQuery function?

Note: the week of year is based on ISO week day calendar. i. e. given 2014 and 1, I need to get 2013-12-30

Anthony
  • 36,459
  • 25
  • 97
  • 163
necromancer
  • 23,916
  • 22
  • 68
  • 115

3 Answers3

2

Using the answer from here Get friday from week number and year in javascript you will get your excepted answer

var w2date = function(year, wn){
    var Day10 = new Date( year,0,10,12,0,0),
        Day4 = new Date( year,0,4,12,0,0),
        weekmSec = Day4.getTime() - Day10.getDay() * 86400000;  // 7 days in milli sec 
    return new Date(weekmSec + ((wn - 1)  * 7 ) * 86400000);   
};

w2date(year, week);

calculating total milli seconds of the given week count (weekmSec + ((wn - 1) * 7 ) * 86400000) and then passing it to Date object will give you the expected answer

Community
  • 1
  • 1
999k
  • 6,257
  • 2
  • 29
  • 32
1

Use a library like momentjs

var year = 2013, weeks = 38;
var m = moment("01-01-" + year, "DD-MM-YYYY");
m.add('weeks', weeks - 1)
console.log(m.toDate())

Demo: Fiddle

Using js

var year = 2013, weeks = 38;
var d = new Date(year, 0, 1);
d.setDate((weeks - 1) * 7)

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

Try this snippet of code:

var d = new Date("January 1, 2013 00:00:00");
d.setDate(259);
alert(d);

259 represents 37 weeks * 7.

Hope this helps

user2685803
  • 271
  • 1
  • 3
  • ISO weeks start on week that has the first Thursday of the year, so not necessarily 1 January. – RobG Sep 11 '13 at 06:30