javascript - How to check whether the date is in past or not?and How to get difference between two dates? by input tag type date and type time -


restaurant app booking table feature.

date input <input type="date"> , <input type="time">

what need.

1.how check whether given/input date , time in past or not.if past not valid,if future valid booking.

2.how difference between 2 dates , times.so can show time left booked table,and user allowed table within booked date , time mentioned.(may setinterval())

html

    <html>     <head>     </head>     <body>     <table id="tdatetime">       <tr><td>select date</td><td>select time</td></tr>        <tr><td><input type="date" id="bdate"></td><td><input type="time" id="btime"></td></tr>     </table>  <input type="button" id="bdtbtn" onclick="getbdtrl(this)" value="book now"></input>     </body>     </html> 

js

function getbdtrl(bookbtn)         {         var bdate=$("#bdate").val();             var btime=$("#btime").val();              var = new date();                     var selecteddate=new date(bdate);                     var selectedtime=new date(btime);                     alert(btime);//returns example- 2:00                     alert(selectedtime);//returns invalid date                     alert(selectedtime.tostring());//returns invalid date                     alert(selectedtime.totimestring());//returns invalid date                     alert(selectedtime.todatestring());//returns invalid date              //date check working             if(selecteddate<now)                     {                     alert("selected date in past");                     }                     else if(selecteddate>now)                     {                         alert("selected date in future");                         }                     else if(selecteddate==now)                     {                         alert("selected date in present");                        }              //time check not working selectedtime                     if(selectedtime<now)                     {                     alert("selected time in past");                     }                     else if(selectedtime>now)                     {                         alert("selected time in future");                         }                     else if(selectedtime==now)                     {                         alert("selected time in present");                        }          //time check not working btime                     if(btime<now)                     {                     alert("selected time in past");                     }                     else if(btime>now)                     {                         alert("selected time in future");                         }                     else if(btime==now)                     {                         alert("selected time in present");                        }     }       //date , time difference not working     var date=new date();                 var tempdate="2015-05-01";                 var d1 = date;//tempdate;//                 //alert("current date d1="+d1);                 var d2 = rlarrbookdatesender;//receiving db2 database data type time booked                 //alert("booked date d2="+d2);                 var datediff = {                          indays: function(d1,d2) {                             var t2 = d2.gettime();                             var t1 = d1.gettime();                              return parseint((t2-t1)/(24*3600*1000));                         }                 };                 alert("diff="+datediff.indays(d1,d2));//no alert executes 

i apologize if looking more answer...but if following right should make sense...

(it's late, believe logic right...)

to compare dates:

var = new date(); var selectdate = new date(bdate); var diff = now.gettime() - selectdate.gettime(); if(diff > 0 || diff == 0) {   // selected date in past or our current time    // (which should tough match down milliseconds) } else if (diff < 0) {   // selected date has not past } 

to time left until future date:

var = new date(); var validfuturedate = new date(bdate); var diff = validfuturedate.gettime() - now.gettime(); // in milliseconds var daydiff = parseint(diff/(1000*60*60*24)); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -