Create Dates Array in Javascript -
i'm attempting create array of dates in javascript based on start date , end date. need date format 2013-04-25
my code apparently doesn't work, can't seem figure out why. can assist?
//get today's date var today = new date(); today.setdate(today.getdate()); //get date last week -7 var prevweek = new date(); prevweek.setdate(prevweek.getdate() -7); //set initial date parameters var fromdate = prevweek; var todate = today; //set date parameters input parameters function setdates() { fromdate =document.getelementbyid('fromdate').value; todate = document.getelementbyid('todate').value; }; var dates = new array(); //create date array function setarray() { for(i = fromdate.getdate(), <= todate.getdate(), i.setdate(i.getdate() +1)) { dates.push(new date(i)); }; }; //format date array function formdates() { for(i = 0, <= dates.length, i++) { var dd = dates[i].getdate(); var mm = dates[i].getmonth()+1; var yyyy = dates[i].getfullyear(); if(dd<10) {dd = '0' + dd}; if(mm<10){mm = '0' + mm}; dates[i] = yyyy + '-' + mm + '-' + dd; }; };
your loop signatures wrong.
they should delimited ;
, not ,
a correct javascript loop made this:
for(var = 0; <= 1000; i++){ }
using var
statements here considered best practice due javascript otherwise hoisting i
variable making global function, or worst case scenario, entire js-file.