Jump to content
  • 0

Another Restaurant Hours Check Question ...


mdupras
 Share

Question

Thanks to some folks here in the forum, my restaurant hours checking is working well. But there's one wrinkle: What if a restaurant closes after midnight?

The way it works now, I have the opening and closing times in the database as 24-hour time. If a restaurant opens at 4 p.m. and closes at 11:30 p.m., it's in the database as 1600 and 2330. I'm using Java to check the current time and convert it to these values, then compare the opening (<=) and closing (>=) times. Here's the Java:

<SCRIPT LANGUAGE=\"JavaScript\">

window.onload = function()
{ 
var currentTime = new Date(); 
var currentHour = currentTime.getHours();
var currentMin = currentTime.getMinutes();
var actualTime = (currentHour * 100) + currentMin;

document.forms[0].Value3_1.value = actualTime;
document.forms[0].Value4_1.value = actualTime; 
}

</SCRIPT>

Works great, except when the opening time is something like 6 p.m. (1800) and the closing time is 2 a.m. (200). It would seem you'd have to perform a different calculation in those cases, but you have to know when that's necessary. One way would be to compare the closing and opening times to each other to see if the closing time is less than the opening time. That would only be true if the closing time were the next day.

But even then, what's the calculation, because it's different depending on what time you check? If the current time is 11 p.m. (2300), then the original calculation fails on checking against the closing time, but if it's 1 a.m. (100), it fails on checking against the opening time; in both cases, though, the restaurant is indeed open.

I attempted some JavaScript that attempts to do three things -- first, it checks to see if the restaurant closes in the wee hours; second, it checks to see if the current time is before midnight but after the opening time and then adjusts the current time so that it won't fail the test and compares that to the close time; and third, if the current time is after midnight but before the close time, it adjusts the current time and compares that to the open time.

Of course, it's not working! Here's the script:

<SCRIPT LANGUAGE=\"JavaScript\">

function checkTime()
{ 
var currentTime = new Date(); 
var currentHour = currentTime.getHours();
var currentMin = currentTime.getMinutes();
var actualTime = (currentHour * 100) + currentMin;
var modifiedTimeAM = actualTime + 2400;
var modifiedTimePM = actualTime - 2400;

if ([@field:dine_CloseHR] < [@field:dine_OpenHR]) && ([@field:dine_OpenHR] < actualTime) && (actualTime < 2359)
{
document.forms[0].Value3_1.value = actualTime;
document.forms[0].Value4_1.value = modifiedTimePM; 
}

else if ([@field:dine_CloseHR] < [@field:dine_OpenHR]) && ([@field:dine_CloseHR] > actualTime) && (actualTime >= 0)
{
document.forms[0].Value3_1.value = modifiedTimeAM;
document.forms[0].Value4_1.value = actualTime; 
}

else
{
document.forms[0].Value3_1.value = actualTime;
document.forms[0].Value4_1.value = actualTime; 
}
}

document.forms[0].onsubmit=checkTime;

</SCRIPT>

So what am I doing wrong -- the math, the logic, the script, the syntax, all four? Anyone have ideas about this?

Thanks!

-- Mike

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...