0

I'm trying to run a popup on my website just during specific times(11 am to 2 pm - client side/server side either). I'm using getHours() to check the current time, but something is wrong - the popup seems to not recognize the condition. Below is the code -

<script type="text/javascript"> 
var now = new Date();
if (now.getHours() > 11 && now.getHours() < 14) 
{
    //popup code
}
</script>

Thanks!

codemode
  • 350
  • 2
  • 4
  • 22
  • 3
    11a-2p on the server or on the client side? – j08691 Nov 05 '15 at 17:11
  • Hi, it can actually be either of them - client or server - it's the same for us. Our server's time is according to Brazil's, thanks – codemode Nov 05 '15 at 17:19
  • 1
    Are you starting this script before 11 and then expecting it to alert after 11? if so, then you should be using a loop. Otherwise you are only checking one time. – Tj Gienger Nov 05 '15 at 17:20
  • 1
    You may want weak inequalities: as-is, yor code is running from 12:00 to 1:59 pm (client time) – Mario Trucco Nov 05 '15 at 17:22

2 Answers2

0

Check the timezone that the code is currently using :) While you may be using something like GTM-5 for example, if your PC is using UTC then it would recognise a different time.

Here is a question I found that could help you find this out: How do you create a JavaScript Date object with a set timezone without using a string representation

For purposes of testing it may be more helpful to set it to now's time so that you can see if it would alert or not. (This is assuming the time isnt already within that range for you)

Community
  • 1
  • 1
-1

That code looks ok, so the problem might be in the popup code and not your date check.

Try using console.log or alert to confirm if the values you're getting are what you expect. console.log is nicer, if you know how to use your console, otherwise try with alerts, with something like:

<script type="text/javascript"> 
var now = new Date();
alert(now.getHours()); //to confirm what's the value you're getting
if (now.getHours() >= 11 && now.getHours() < 14) 
{
    alert('here there be dragons'); //to confirm that the condition is ok
}
</script>
innocuo
  • 114
  • 1
  • 5
  • 1
    The code is actually wrong: it runs from 12:00 to 1:59 pm – Mario Trucco Nov 05 '15 at 17:27
  • ,...for some strange reason, alert(now.getHours()); is not working :( – codemode Nov 05 '15 at 17:29
  • @MarioTrucco you're right, I added the >= so it works from 11. I'm assuming up to 2pm is not including 2pm, so I'm leaving that alone – innocuo Nov 05 '15 at 17:35
  • @codemode if the alert is not working, there's something wrong with previous lines of your code, or with your browser (or maybe you have a setting that hides alerts, in which case you should use console.log). The code is working for me on jsfiddle [link](https://jsfiddle.net/93346r4n/1/) – innocuo Nov 05 '15 at 17:38
  • Works now :) Thank you innocuo. Funny im gonna mark the downvoted answer as accepted haha. Cheers – codemode Nov 05 '15 at 17:55
  • @codemode so what was the error for you? it'd be helpful if you add more details in case other people that have the same problem also get to figure it out. And thanks :) – innocuo Nov 05 '15 at 17:58
  • ah it was not worth mentioning lol, the brackets were messing – codemode Nov 05 '15 at 18:02