validation - Comparing time ranges in javascript -



validation - Comparing time ranges in javascript -

i need validate time picked user, start date must greater 6 , smaller end date must not exceed next day 2 am.

though dont have date input assume should append dates picked times compare, best way accomplish or there smarter 1 ?

here can think of far

function comparetimes(start,end){ var t1 = new date(); var t2 = new date(); var starttime = t1 + " " + start; if (end >= '00:00'){ t2.setdate(t2.getdate()+1); } var endtime = t2 + " " + end; if(date.parse ( endtime ) > date.parse ( starttime )){ alert ("greater than"); homecoming true } }

the next function appends time string 1 give, date, , returns new date object. if pass no date, utilize today’s date.

function setdatetime(timestring, date) { var result = (date ? new date(date) : new date()) , match = /(\d+):(\d+)/.exec(timestring) ; result.sethours(number(match[1]), number(match[2]), 0, 0); homecoming result; }

once you’ve got startdate = setdatetime(start); , enddate = setdatetime(end);, can utilize gethours method figure out if it’s before or after 2 or 6 pm.

n.b. should check if hr < 6, , so, add together day (enddate.setdate(enddate.getdate() + 1);, otherwise end date before start date. :-)

javascript validation datetime

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -