Skip to content

Instantly share code, notes, and snippets.

@Abhinav1217
Last active January 20, 2022 08:42
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Abhinav1217/5038863 to your computer and use it in GitHub Desktop.
Save Abhinav1217/5038863 to your computer and use it in GitHub Desktop.
Get date range based on week number.
/**
* Get the date and days within a week from week number.
* eg: date range for 8th week in 2013 is 17th Feb to 23rd Feb. This
* code snippet will give you.
*
* It is not my code completely, Bit of modification from something
* i found on net. Cant find it anymore so keeping a backup.
*
* @param {[Integer]} weekNo [From week 1 to Week 52/53 based on the system date setting]
* @return {[Date]} [description]
*/
function getDateRangeOfWeek(weekNo){
var d1 = new Date();
numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1);
d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
var weekNoToday = d1.getWeek();
var weeksInTheFuture = eval( weekNo - weekNoToday );
d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture ));
var rangeIsFrom = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear();
d1.setDate(d1.getDate() + 6);
var rangeIsTo = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear() ;
return rangeIsFrom + " to "+rangeIsTo;
};
@gururamp30
Copy link

Hello there,

This code snippet is exactly what I was looking for. However, I don't think that the function getWeek(); exists and it's coming up as an error in your code. If you wrote the getWeek(); function seperately, it'd be great if you could share that as well. Thanks!

@kryss88ltj
Copy link

You can use something like this:

Date.prototype.getWeek = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
}

@Abhinav1217
Copy link
Author

https://gist.github.com/Abhinav1217/eb001d2c20b6081e5563

Based on ISO-8601, same thing which is used in PHP's date->format("W") use.

@unitedsoftworks
Copy link

This doesn't seem to work when the date is "week 1 of 2100." Possibly due to leap years?

@Abhinav1217
Copy link
Author

@unitedsoftworks

Year 2100 is not a leap year, centuries should be divisible by 400. Most likely cause is y2k38 bug. Basically, current Unix time can not store time after 19 January 2038, about 3:14 in the morning UTC. so year 2100 is basically rollback of the counter to year 2032 (2100-2038 = 62 years, added on 1970 is 2032.)

Check if your result for 2100 is same for 2032 minus 2 weeks, That can be the main cause.
Also, What is your implementation of getWeek() function, The one by @Ryss above or the one I gave in another gist linked above, or something else?

I wrote that piece of code about 5 years ago fresh out of college, I checked for a lot of leap year and centuries, But I didn't knew about y2k38 bug at that time. It was a quick hack at that time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment