Skip to content

Using setUTChours to handle user timezone difference

I’m trying to create a simple piece of javascript that will set my opening hours via setUTC, and then pass through a users time accessing a page and declare whether they’re accessing it in my opening or closing hours.

When I run this attached to my page, I’m constantly getting the message ‘I’m not currently available’.

var currentDate = new Date(); var currentHour = currentDate.getHours(); console.log(currentHour);

var openTime = new Date(); openTime.setUTCHours(9,0,0); var closingTime = new Date(); closingTime.setUTCHours(19,0,0);

var emailmessage = “You’re good to email me!”;

if (currentHour < openTime || currentHour > closingTime) {
emailmessage = “I’m not currently available”; }

Any help would be appreciated, I’m new to coding/JS however on reading the literature I’ve made the above and I’m not quite there.


Try this:

const currentHour = new Date().getUTCHours();
let emailmessage = "You're good to email me!";
if (currentHour < 9 || currentHour > 19) {
    emailmessage = "I'm not currently available";