Using moment.js to check if local time is between 2 other time zones times


Similar questions have been asked but for whatever reason I just can’t wrap my head around this. I want a user anywhere in the world to see whether a shop is open or closed on my site. The shop’s hours are 8am-8pm M-F, and 8am-7pm Sat-Sun.

So basically the code needs to grab the users local time and compare that to the current time in CT, and see if the shop is open. This is made a bit more complicated by the fact that if its 7pmCT on Tuesday night, it could be Wednesday morning in a far eastern time zone, so now you need to compare the days too.

All the searching I did found bits and pieces of similar questions/answers but I just can’t wrap my head around how to put it all together to come up with a solution. I started by trying this:

var currTimeTokyo = moment.tz(moment(), 'Asia/Tokyo').format();
var currTimeCT = moment.tz(moment(), 'America/Chicago').format();
console.log(currTimeTokyo);
console.log(currTimeCT);

and that gives:

2021-10-30T00:51:42+09:00
2021-10-29T10:51:42-05:00

Those are correct, but now how do I compare to today’s open/closed hours? I need to check if the Tokyo time is between open and close in CT, but the open/close times change depending on what day it is, and Tokyo could be a different day from CT anyways. How would you account for all of that?

EDIT: I should note I’m importing and using moment.js and moment-timezone-with-data.js

Source: JavaSript – Stack Overflow

October 29, 2021
Category : News
Tags: compare | javascript | momentjs | time

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.