Check if the store is open

A fine distinction, except that Javascript will automatically apply said offset when referencing non-UTC date functions, so it’s as good as any to say that that Date object rides the offset implicitly.

You still need to find the offset locally vs target timezone to adjust the numbers, and that will require a date object to pull its “non-existent” timezone out.

It is much less confusing if you just think of a Date object as containing nothing other than a value in milliseconds.

As demonstrated here, to determine whether the store is open you do not need to mess about with offsets:

Of course you may wish to get the JavaScipt to update the open/closed status say every minute.

1 Like

I may be misunderstanding post 17 then. If the intent is to show time in Ontario time regardless, then yes (but you could… yaknow… just write the opening time into the HTML at that point, because it’s static?)

If the intent is to show when the store opens in the user’s local time, then you’ve got some math to do.

Yes that’s what I am trying to do. My store is open from 9-5 in local Toronto Ontario time. With user who comes to my site there are no issues but for users who come from different time zone I feel like I need to do something because userA will end up under closed store and userB will end up under //do something here because if I take London time the store is open when in reality it isn’t because its 8:10 here.

//user A someone from Toronto current time 8:10 AM the store will be closed he will see just current time
if (userA.hours >= 9 && userA.hours <= 5){
//do something here
else {
//store closed show Toronto time
//user B from London UK current time 1:13 pm
//will end up under first if statement

Archibald’s example gives you exactly that. It takes the UTC time, translates that to Toronto local time, and determines whether or not the store would be open or not. It’s a simple 9am-5pm check, not accounting for weekends or holidays, but the idea is there.

To hopefully clarify it a bit more, I’ve added two more local times to his example to show what time it is in London and California, but it’s all working off the same UTC date object.


Yes I was aware of that but your code is checking whether the store is open off the Toronto hours. What if instead you took the hour this way?

let hour = timeNow.toLocaleString(‘en-CA’, {timeZone: ‘Europe/London’, hourCycle:‘h24’, hour:‘numeric’});

would it not show that the store is closer because hours returned would be 6 pm?

Why would you do that?

It really doesn’t matter what time it is where the user is at the moment.
What is important is what time it is in Toronto at that moment because that’s where the store is.

Think of it this way.

  • It’s 6pm in London. Can I call the store in Toronto right now? Yes, because it’s 1pm in Toronto and the store is open.
  • It’s 11am in London. Can I can the store in Toronto right now? No, because it’s 6am in Toronto and the store is closed.

So you need to convert the current UTC into the Toronto time zone and check the hours to see if it falls within the window it’s open.

yes that’s exactly what i am trying to do. get the time in London, convert it to Toronto and check against opening hours

That’s the piece I’m not sure you’re getting.

UTC is considered the baseline time. The older term was Greenwich Mean Time. It is the same on EACH AND EVERY COMPUTER at the same point in time.

If you want a user to be able to tell if the store is open at any one point in time, you need to get the store time and the users times into the same time zone. So you have three choices you can make.

  1. Convert the user time to reflect the appropriate time in the stores time zone, then compare the converted time to be between the store’s hours of operations
    • User time from 6PM (London) to 1PM (Toronto).
    • Is 1PM between 9AM and 5PM? Yes
  2. Convert the store time to the user time zone. So, convert the start time and end times to the users time zones, then check to see if the user time is between those converted times.
    • Convert the opening hour from Toronto (9AM) to London time (2PM)
    • Convert the closing hour from Toronto (5PM) to London time (10PM)
    • Is 6PM between 2PM and 10PM? Yes
  3. Convert the store times AND the user time into UTC time. So convert the three values to UTC time, then compare to see if within the time range.
    • Convert the user time from London (6PM) to UTC (5PM).
    • Convert the opening hour from Toronto (9AM) UTC (1PM).
    • Convert the closing hour from Toronto (5PM) to UTC (9PM)
    • Is 5PM between 1PM and 9PM? Yes

Which method seems like less work to you? Seems like option 1 to me. Convert the users time to Toronto time, then check it against the Toronto store hours.

1 Like

I normally have a timestamp field in the table of my database. Timestamp datetime type in my understanding is Unique in value and should never be duplicated the same as Primary Key Auto Increment datatype integer in a database table… Regardless of where it (the date & time) came from the timestamp in your server database will be the same as the time data was received will always be the same. From there, have a function into your server side to convert the timestamp to the location where it came from so your data in your database will always be consistent.

Thank you Dave. #1 make sense as well. I think I got it now.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.