I have a scenario where I am trying to achieve the following:
I have the below variables:
StartDate = 16/02/2016
EndDate = 18/02/2016
StartTime 09:00:00
FinishTime 17:00:00
from this I am trying to make a function that can take the StartDate
and EndDate
, and store all the values within a multi-dimensional array. so the array would read:
startDate endDate
16/02/2016 09:00:00 16/02/2016 17:00:00
17/02/2016 09:00:00 16/02/2016 17:00:00
18/02/2016 09:00:00 16/02/2016 17:00:00
currently I am using 2 septate functions and 2 separate arrays to get a result however I dont feel this is good practice or efficient, but cannot think of a way of making this code better, please could you take a look at it and suggest anything better?
using window.alert()
on the array is good enough for now :) thankyou for the help
the code (REVISED):
function getDates(startDate, stopDate) {
var dateArray = new Array();
var currentDate = startDate;
while (currentDate <= stopDate) {
dateArray.push(currentDate)
currentDate = currentDate.addDays(1);
}
return dateArray;
}
Date.prototype.addDays = function (days) {
var dat = new Date(this.valueOf())
dat.setDate(dat.getDate() + days);
return dat;
}
myapp.TestScreen.Test_execute = function (screen) {
var sDate = new Date(screen.StartDate);
var eDate = new Date(screen.EndDate);
var nightCount = -1;
var fullDateArray = [];
var dateArray = getDates(sDate, eDate);
for (i = 0; i < dateArray.length; i++) {
var tempSdate = new Date (dateArray[i])
var tempEdate = new Date(dateArray[i])
tempSdate.setHours(9);
tempSdate.setMinutes(0);
tempEdate.setHours(17);
tempEdate.setMinutes(0);
fullDateArray.push(tempSdate);
fullDateArray.push(tempEdate);
nightCount++;
}
for (i = 0; i < fullDateArray.length; i++) {
console.log(fullDateArray[i]);
}
code above used from: javascript - get array of dates between 2 dates