0

I'm working on a little project for a small business website and I found a helpful tutorial for a script. The script will show an open or closed sign depending on the current time I changed a couple of things but now it's working just fine, except for one small thing...

The time setting they use is wrong. I live in the Netherlands, and the timezone here is UTC+1. The javascript is working on UTC.

This is the code

localTime=new Date();
utc1Time=new Date(localTime.getTime() + (localTime.getTimezoneOffset() + 120) * 60000); 
hours=utc1Time.getHours(); 
minutes=utc1Time.getMinutes(); 
today=utc1Time.getDay();

Now I'm wondering what the code should be to get the correct timezone. And just so you know, I'm new to Javascript. Thanks in advance!

edit:

this is an example how I use the time so show the diffrent sign

if (today==6) {
if (hours>=1&&minutes>=4) {
    if (hours<=23&&minutes<=30) {document.write(statusopen);
    }
    else {document.write(statusclosed);
    }
}
else {document.write(statusclosed);
}
} 
  • 1
    Use `localTime.getHours` instead of `utc1Time.getHours`. – Barmar Nov 07 '15 at 00:45
  • Your code is deliberately switching to UTC. Don't do that if you don't want UTC times. – Barmar Nov 07 '15 at 00:46
  • like this? function displayState() { localTime=new Date(); hours=localTime.getHours(); minutes=localTime.getMinutes(); today=localTime.getDay(); – Anton van Elburg Nov 07 '15 at 00:51
  • It is not working. it won't respond to any time I set as condition. – Anton van Elburg Nov 07 '15 at 00:57
  • What do you mean by "won't respond to any time"? I don't see anything about responding to a condition in the question. The name of your function suggests that it's just used to display the time, and that should work. If there's more to the problem than that, update the question to show what you're talking about. – Barmar Nov 07 '15 at 01:00
  • Sorry my mistake, I made a fault while changing the code. Now I found the fault but it is still working on UTC.So when I change the opening time to my current time, it showes closed. but when I set it back one hour, it showes open. – Anton van Elburg Nov 07 '15 at 01:06
  • See the duplicate answer. Use one of those libraries, such as [moment-timezone](http://momentjs.com/timezone/), with the `"Europe/Amsterdam"` identifier for the Netherlands. – Matt Johnson-Pint Nov 07 '15 at 05:09
  • Thanks @MattJohnson ! The problem is solved now! – Anton van Elburg Nov 07 '15 at 12:24

1 Answers1

0

If you want a function to tell you whether the current time is within the range of available hours, you could try the following method below.

var hoursOfOperation = [
    [ [8, 15], [17, 30] ], // Sunday
    [ [8, 15], [17, 30] ], // Monday
    [ [8, 15], [17, 30] ], // Tuesday
    [ [8, 15], [17, 30] ], // Wednesday
    [ [8, 15], [17, 30] ], // Thursday
    [ [8, 15], [17, 30] ], // Friday
    [ [8, 15], [17, 30] ]  // Saturday
];

var date = new Date();
var timezone_EST = -300;
var open = isOpen(hoursOfOperation, date, timezone_EST);

document.body.innerHTML = open ? 'OPEN' : 'CLOSED';

function isOpen(hoursOfOperation, date, timezoneOffset) {
    var hasOffset = timezoneOffset !== undefined;
    var hour = hasOffset ? date.getUTCHours() : date.getHours();
    var minute = hasOffset ? date.getUTCMinutes() : date.getMinutes();
    var day = hasOffset ? date.getUTCDay() : date.getDay();;    
    var offsetHours = hasOffset ? Math.floor(timezoneOffset / 60) : 0;
    var offsetMinutes = hasOffset ? timezoneOffset % 60 : 0;
    var incrementHour = 0;
    var incrementDay = 0;

    if (offsetMinutes != 0) {
        incrementHour = minute + offsetMinutes > 60 ? 1 : minute + offsetMinutes < 0 ? -1 : 1;
        minute = mod(minute + offsetMinutes, 60);
    }

    if (offsetHours != 0) {
        incrementDay = hour + offsetHours > 24 ? 1 : hour + offsetHours < 0 ? -1 : 1;
        hour = mod(hour + offsetHours, 24);
    }

    day = mod(day + incrementDay, 7);

    console.log(day, hour, minute);

    return (function(dayOfWeek) {
        var minTime = dayOfWeek[0];
        var minHour = minTime[0];
        var minMinute = minTime[1];
        
        if (hour < minHour) return false;
        if (hour === minHour && minute < minMinute) return false;
      
        var maxTime = dayOfWeek[1];
        var maxHour = maxTime[0];
        var maxMinute = maxTime[1];
      
        if (hour > maxHour) return false;
        if (hour === maxHour && minute > maxMinute) return false;

        return true;
    }(hoursOfOperation[day]))
}

function mod(n, m) {
    return ((n % m) + m) % m;
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132