0

I have some code that checks if a user falls within a specific time (9.00am - 17.30pm) Monday to Friday.

function checkTime() {
    var d = new Date(); // current time
    var hours = d.getHours();
    var mins = d.getMinutes();
    var day = d.getDay();

    return day >= 1
           && day <= 5
           && hours >= 9 
           && (hours < 17 || hours === 17 && mins <= 30);
}

This is working correctly but it checks the time based on the users browser time.

What I want to do is have it check 9.00am - 17.30pm UK Time Only.

So if you are viewing the site/script from America at say 10.00am (UTC-7) you would be outside of the time checker because it would be 18.00pm (UTC+1) UK time.

So I guess what I'm trying to do is get the users timezone and then convert it to UK time and then run my checkTime() script?

Oshadha
  • 546
  • 10
  • 25
BoneStarr
  • 195
  • 1
  • 3
  • 15
  • 3
    Related: http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript. The tricky part from there would be knowing the UK time as we run daylight savings throughout the year so the difference to GMT could mean your calculated time is out by 1 hour. I would recommend you retrieve the UK time from a server and check there instead of fudging a client side solution. Especially if you rely on the client clock being set correctly. – Rory McCrossan Jun 21 '16 at 08:56
  • @RoryMcCrossan: `+new Date()` is not affected by DST, and happens to return the uk's timestamp... – dandavis Jun 21 '16 at 08:59
  • The OP wants to know the time in the UK from the client date. All you can get is the time in GMT (by subtracting the timezone offset), whereas for part of the year the UK runs on BST (ie GMT+1) – Rory McCrossan Jun 21 '16 at 09:00
  • @RoryMcCrossan: good call on BST, but remember that all Date valueOfs are GMT without subtraction, no need to worry about local time zones, which are just for the non-binding toString/toLocaleXXX anyway... – dandavis Jun 21 '16 at 09:11
  • @RoryMcCrossan would something like this be the best way? var offset = new Date().getTimezoneOffset(); if ((offset === 0 || offset === -60)) { checkTime(); } – BoneStarr Jun 21 '16 at 09:39
  • @dandavis - You do indeed need to worry about time zones in this scenario, because the business logic is based on local time - not GMT. – Matt Johnson-Pint Jun 21 '16 at 16:44
  • 1
    @BoneStarr - no, that wouldn't work because the `offset` will be that of the user's local time zone, which won't necessarily be the one you are interested in. – Matt Johnson-Pint Jun 21 '16 at 16:48

1 Answers1

1

As Rory mentioned in comments, if your point of reference is a specific time zone (UK Time in this case), then you will need logic to understand the nature of that time zone - especially with regard to time zones that have daylight saving time transitions, or other changes to their time zone offset.

The local time in the UK alternates between GMT (UTC+00:00) and BST (UTC+01:00), and currently follows the European Union transition rules (Last Sunday in March to Last Sunday in October, both at 01:00 UTC).

You certainly could hard-code those rules into your own logic, but you would probably find the code to be overly complex and difficult to maintain. A better approach is to use a library that is designed for this purpose. I list several of them here.

For example, using the moment-timezone library, which is a companion library for the popular moment.js library, you can simply do this:

function checkTime() {

    // now in the UK
    var m = moment.tz("Europe/London");

    // use moment.js getters
    var hours = m.hour();
    var mins = m.minute();
    var day = m.day();

    // your original logic retained
    return day >= 1
           && day <= 5
           && hours >= 9 
           && (hours < 17 || hours === 17 && mins <= 30);
}
Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575