Created
November 22, 2015 01:10
-
-
Save 5iDS/78f1ce47a520e0f7ab61 to your computer and use it in GitHub Desktop.
Get weeks of year for given period
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
Date.prototype.addDays = function(days) { | |
var dat = new Date(this.valueOf()); | |
dat.setDate(dat.getDate() + days); | |
return dat; | |
}; | |
// Source: http://stackoverflow.com/questions/497790 | |
function convertDate ( d ) { | |
// Converts the date in d to a date-object. The input can be: | |
// a date object: returned without modification | |
// an array : Interpreted as [year,month,day]. NOTE: month is 0-11. | |
// a number : Interpreted as number of milliseconds since 1 Jan 1970 (a timestamp) | |
// a string : Any format supported by the javascript engine, like | |
// "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc. | |
// an object : Interpreted as an object with year, month and date attributes. **NOTE** month is 0-11. | |
return ( | |
d.constructor === Date ? d : | |
d.constructor === Array ? new Date(d[0],d[1],d[2]) : | |
d.constructor === Number ? new Date(d) : | |
d.constructor === String ? new Date(d) : | |
typeof d === "object" ? new Date(d.year,d.month,d.date) : | |
NaN | |
); | |
} | |
function getDates(startDate, stopDate) { | |
var dateArray = []; | |
var weekArray = []; | |
var currentDate = convertDate(startDate); | |
var weekday = new Array(7); | |
var weekDayCounter = 1; | |
weekday[0]= "Sunday"; | |
weekday[1] = "Monday"; | |
weekday[2] = "Tuesday"; | |
weekday[3] = "Wednesday"; | |
weekday[4] = "Thursday"; | |
weekday[5] = "Friday"; | |
weekday[6] = "Saturday"; | |
stopDate = convertDate(stopDate); | |
while (currentDate <= stopDate) { | |
if( weekday[currentDate.getDay()] == "Sunday" ) { | |
weekDayCounter++; | |
} | |
dateArray.push({ | |
'week' : weekDayCounter, | |
'date' : currentDate | |
}); | |
//currentDate = convertDate(currentDate); | |
currentDate = currentDate.addDays(1);//convertDate(currentDate).addDays(1); | |
//console.log(currentDate); | |
} | |
return { | |
'dates': dateArray, | |
'weeks': weekDayCounter | |
}; | |
} | |
//getDates('2013-08-01', '2015-10-31'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment