Search code examples
node.jsdatetimetimezoneiana

Looking for a way to retrieve timezones in which the current time is x


I am looking for a way to obtain a list of timezones in which the current time is a variable (it has to be DST dependant as well).

For example I need to push messages to users for which the local time is 18:45. I can fetch from our database a list of users by timezone. So I plan a scheduled event that would wake up at every 15 minutes, it will need to check in which timezones it is currently 18:45 in order to be able to retrieve the right users.

Our environment is node js. The way I am thinking about is preferably a web service or some utility that does it all for me including the updates from iana timezones database. But I can handle less convenient ways if there's no such alternative.

Thanks in advance.


Solution

  • Here is one simple way to do it with moment-timezone (I believe it handles things like daylight savings time etc., not 100% sure -- but certainly will do it better than one could do starting from scratch alone.. see their github repo for issues -- its a complex topic):

    var moment = require('moment-timezone');
    var tm = moment();
    
    var toFind = tm.format('h:mm a');
    
    var userZones = ['America/Los_Angeles', 'America/New_York'];
    for (var i=0; i<userZones.length; i++) {
      var fmt = tm.tz(userZones[i]).format('h:mm a');
      if (fmt === toFind) console.log('matched zone: ' + userZones[i]);
    }