Posted By

jamesming on 07/27/08


Tagged

date week


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

SimonJ621
IMPGTX


get date range from week number in year


 / Published in: JavaScript
 

  1. function getDateRangeOfWeek(weekNo){
  2. var d1 = new Date();
  3. numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1);
  4. d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
  5. var weekNoToday = d1.getWeek();
  6. var weeksInTheFuture = eval( weekNo - weekNoToday );
  7. d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture ));
  8. var rangeIsFrom = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear();
  9. d1.setDate(d1.getDate() + 6);
  10. var rangeIsTo = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear() ;
  11. return rangeIsFrom + " to "+rangeIsTo;
  12. };

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: tonyb on February 23, 2010

slightly modified to return array...

{... var range = new Array(); range[0] = rangeIsFrom; range[1] = rangeIsTo; return range; }

Then call from another function... {... var range = getDateRangeOfWeek(week-1); var first = new Date(range[0]); var second = new Date(range[1]); ...}

Posted By: SimonJ621 on June 23, 2010

You could add a date offset as a parameter. Something like this ...

function getDateRangeOfWeek(weekNo,dayOffset) { dayOffset = typeof(dayOffset) == 'int' ? dayOffset : 0; var d1 = new Date() numOfdaysPastSinceLastMonday = eval(d1.getDay() - dayOffset); ... }

0 = Sunday 1 = Monday 2 = Tuesday etc.

Posted By: SimonJ621 on June 23, 2010

You could add a date offset as a parameter. Something like this ...

function getDateRangeOfWeek(weekNo,dayOffset) { dayOffset = typeof(dayOffset) == 'int' ? dayOffset : 0; var d1 = new Date() numOfdaysPastSinceLastMonday = eval(d1.getDay() - dayOffset); ... }

0 = Sunday 1 = Monday 2 = Tuesday etc.

Posted By: SimonJ621 on June 25, 2010

I've also had to change the following from my previous comment (2 comments :/)

change dayOffset = typeof(dayOffset) == 'int' ? dayOffset : 0;

to dayOffset = typeof(dayOffset) == 'number' ? dayOffset : 0;

You need to login to post a comment.