How to check and set if current time is out of specific range [Javascript/jQuery] -
i submit forms time time must in specific range time. if out of specific range time, generated in new time in specific range time.
my specific range time 08:30-17:30 on weekday.
if current time not on weekday, sets day monday @ 08:30.
ex. if current time saturday 12:00 ,sets monday 08:30
if current time less 08:30, sets time 08:30 on day.
ex. if current time tuesday 01:00 ,sets tuesday 08:30
if current time more 17:30, sets time 08:30 on tomorrow.
ex. if current time tuesday 20:00 ,sets wednesday 08:30
if current time on friday , time more 17:30, sets time 08:30 on monday.
ex. if current time friday 20:00 ,sets monday 08:30
could please help? thank you.
function createddatefunction() { var days = ['sun','mon','tue','wed','thur','fri','sat']; var = new date(); var day = days[now.getday()] var date = now.getdate(); var month = now.getmonth()+1; var year = now.getfullyear(); var hour = now.gethours(); var minute = now.getminutes(); var second = now.getseconds(); if(month.tostring().length == 1) { var month = '0'+month; } if(day.tostring().length == 1) { var date = '0'+date; } if(hour.tostring().length == 1) { var hour = '0'+hour; } if(minute.tostring().length == 1) { var minute = '0'+minute; } if(second.tostring().length == 1) { var second = '0'+second; } var datetime = day+' '+date+'/'+month+'/'+year+' '+hour+':'+minute; var nowday = day; var nowdate = date+'/'+month+'/'+year; var nowtime = hour+'.'+minute; document.getelementbyid('idcreatedate').value= datetime; var t = settimeout(function(){createddatefunction()},500); sessionstorage.setitem('adate',date); sessionstorage.setitem('amonth',month); sessionstorage.setitem('ayear',year); sessionstorage.setitem('nowday',nowday); sessionstorage.setitem('nowdate',nowdate); sessionstorage.setitem('nowtime',nowtime); } function checkfunction() { var date = sessionstorage.getitem('adate'); var month = sessionstorage.getitem('amonth'); var year = sessionstorage.getitem('ayear'); var nowday1 = sessionstorage.getitem('nowday'); var nowdate1 = sessionstorage.getitem('nowdate'); var nowtime1 = sessionstorage.getitem('nowtime'); alert("day: " + nowday1); alert("date: " + nowdate1); alert("time: " + nowtime1); if(nowday1!='sat'&&nowday1!='sun'){ if(nowtime1>08.30 && nowtime1<17.30){ console.log("yes, time in range"); //next, plus priority date(high=2days,medium=3days,low=4days) , check again sure not on sat, sun }else{ console.log("no, time out of range"); //check <8.30 or >17.30 next //if(nowtime1<08.30){ //set day @ 08.30 //}else if(nowtime1>17.30){ //set day+1 , date+1 @ 08.30 //} } }else{ console.log("have set mon 08:30"); //nowday1 = 'mon'; //nowtime1 = '08.30' } }
this function do:
function checkrange(d) { var hours = d.gethours(); var mins = d.getminutes(); var day = d.getday(); var dayofweek = 1; if(day >= 1 && day <= 5){ if(hours < 8 || (hours === 8 && mins <= 30)){ d.sethours(8, 30, 00); return d; } else if(hours > 17 || (hours === 17 && mins > 30)){ (day === 5) ? d.setdate(d.getdate() + (dayofweek + 7 - d.getday()) % 7) : d.setdate(d.getdate()+1); d.sethours(8, 30, 00); return d; } } else{ d.setdate(d.getdate() + (dayofweek + 7 - d.getday()) % 7); d.sethours(8, 30, 00); return d; } return 'time in range'; } console.log(checkrange(new date('09/12/2015 20:30:00')));
Comments
Post a Comment