Last active
January 20, 2022 08:42
-
-
Save Abhinav1217/5038863 to your computer and use it in GitHub Desktop.
Get date range based on week number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.