javascript - Create array of available time slots between two dates -
i weak on front end work , need little here.
i developing basic form gives users available schedule options booking appointment during next 3 days inclusive.
so have far written:
var today = new date(); var lastday = new date(today); lastday.setdate(today.getdate() + 4); var daterange = getalldays(today, lastday); function getalldays(today, lastday) { var s = new date(today); var e = new date(lastday); var = []; //this gets start time days while (s < e) { //if day saturday hours change if(s.getday() == 0) { //if sunday skip } else if (s.getday() == 6) { //push start onto array a.push(setworkinghours(s, '10')); //push end onto array a.push(setworkinghours(s, '20')); } else { //push start onto array a.push(setworkinghours(s, '13')); //push end onto array a.push(setworkinghours(s, '20')); } s = new date(s.setdate(s.getdate() + 1)) } return a; }; function setworkinghours(date, hour) { var datetime = new date(); datetime.setdate(date.getdate()); datetime.sethours(hour, '0', '0'); return datetime; } alert(daterange.join('\n'));
now know use refinement , open improvements.
so above code gets me array of days start , stop time. i'm struggling figure out how going array of hours within each days start , stop time.
further once have hours have query google calendar returns events parse array.
upcoming events: bob builder (2015-08-07t10:00:00-08:00) john doe (2015-08-08t11:00:00-08:00) mary jane (2015-08-10t18:00:00-08:00)
finally need "intersect" available array booked array , return left.
as appointments themselves. if person picked time schedule 2 hour block. appointments can start @ top or bottom of hour.
i use along lines of gettimeslotsforday
function below array of available start times given day.
var googlecalenderappointments = null; var today = new date(); var lastday = new date(today); lastday.setdate(today.getdate() + 4); function checkgooglecalendarconflict(date) { var hasconflict = false; if (!googlecalenderappointments) { //logic scheduled appointments } //iterate through relevant scheduled appointments //if argument `date` has conflict, return true //else, return false return hasconflict } function gettimeslotsforday(date) { var timeslots = [] var daystart = new date(date) var dayend = new date(date) switch (date.getday()) { case 0: //sunday return timeslots; case 6: //saturday daystart.sethours(10, 0, 0, 0) dayend.sethours(20, 0, 0, 0) break; default: daystart.sethours(13, 0, 0, 0) dayend.sethours(20, 0, 0, 0) } { if (!checkgooglecalendarconflict(daystart)) { timeslots.push(new date(daystart)) } daystart.sethours(daystart.gethours(), daystart.getminutes() + 30) } while (daystart < dayend); return timeslots } var message = "" (var = new date(today); < lastday; i.setdate(i.getdate() + 1)) { message += i.todatestring() + ":\n" message += gettimeslotsforday(i).map(function(it) { return it.totimestring(); }).join(",\n") + "\n"; } alert(message)
Comments
Post a Comment