餐厅应用预订餐 table 功能。
日期输入<input type="date"> and <input type="time">
我需要什么。
1.如何检查给定/输入的日期和时间是否是过去的。如果过去无效,如果 future 有效则预订。
2.如何获取两个日期和时间之间的差异。这样我就可以显示预订餐 table 的剩余时间,并且允许用户在提到的预订日期和时间内获得餐 table 。(可能是 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 now = new Date();
var selectedDate=new Date(bdate);
var selectedTime=new Date(btime);
alert(btime);//returns for 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 is working
if(selectedDate<now)
{
alert("Selected Date is in Past");
}
else if(selectedDate>now)
{
alert("Selected Date is in Future");
}
else if(selectedDate==now)
{
alert("Selected Date is in Present");
}
//Time Check is not working by selectedTime
if(selectedTime<now)
{
alert("Selected Time is in Past");
}
else if(selectedTime>now)
{
alert("Selected Time is in Future");
}
else if(selectedTime==now)
{
alert("Selected Time is in Present");
}
//Time Check is not working by btime
if(btime<now)
{
alert("Selected Time is in Past");
}
else if(btime>now)
{
alert("Selected Time is in Future");
}
else if(btime==now)
{
alert("Selected Time is in Present");
}
}
//Date and 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 from db2 database data type is time which is already 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
最佳答案
您需要从 DOM 元素中获取值,new Date 只接受字符串或数字或一系列数字,而不接受 DOM 元素。尝试在日期和时间字段中输入一个值,然后在控制台中输入以下代码。
alert( new Date( bdate.value + ' ' + btime.value ) - new Date > 0? 'future' : 'past');
关于javascript - 如何检查日期是否过去?以及如何获取两个日期之间的差异?按输入标签类型日期和类型时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30252186/