I am working on creating a form which should only be submitted during opening hours of a business. Using the alert calls to show me the times of each of the date variables I can see that the times are all as expected but somehow the form is submitted no matter what the time. I would like to ask for help with getting this to work as I intend please. I have experimented with changing the expressions within the if-else statement already with no success.
To explain the dates; opening hours during lunch are 12 noon till 2 pm and opening hours during evening are 5 pm till 11 pm.
This is the javascript function to check whether or not to submit the form:
function startOrder()
{
var now = new Date();
//var d = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var lunchOpen = new Date(2015, 11, 5, 12, 00, 00, 00);
var lunchClose = new Date(2015, 11, 5, 14, 00, 00, 00);
var eveningOpen = new Date(2015, 11, 5, 17, 00, 00, 00);
var eveningClose = new Date(2015, 11, 5, 23, 00, 00, 00);
alert("NOW: " + now);
alert("LUNCH OPEN: " + lunchOpen);
alert("LUNCH CLOSE: " + lunchClose);
alert("EVENING OPEN: " + eveningOpen);
alert("EVENING CLOSE: " + eveningClose);
if ((now > lunchOpen) && (now < lunchClose) == false)
{
document.getElementById('error_Message').innerHTML = "Sorry, we're not open at the moment. You can check our opening times <a href=\"information.html\" class=\"content_Links\">here</a>.";
}
else if ((now > eveningOpen) && (now < eveningClose) == false)
{
document.getElementById('error_Message').innerHTML = "Sorry, we're not open at the moment. You can check our opening times <a href=\"information.html\" class=\"content_Links\">here</a>.";
}
else
{
document.forms["startOrderForm"].submit();
}
}
At some point I will need to change all the date variables which hold the current date's opening hours so that they contain the current date's year, month and day like this...
var lunchOpen = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 12, 00, 00, 00);
I will note that I am aware this system will not be completely reliable because a client's device may not have the correct time set. I have a solution to this that I will implement later.
Many thanks for all the help. If you need any extra details just leave a comment. I do hope I am not making a stupid mistake here.