Is there a way in Javascript to check if a location with a given long/lat and city uses Daylight Savings?
I'm currently traversing through a list of objects from different locations and need to calculate their date range in seconds.
Is there a way in Javascript to check if a location with a given long/lat and city uses Daylight Savings?
I'm currently traversing through a list of objects from different locations and need to calculate their date range in seconds.
We can use the tz-lookup module to determine an IANA timezone from the latitude and longitude of the location, like so.
I've switched to using the Luxon module to get the UTC offset in minutes for each location. Once you have this you should be able to get the difference between each city.
let IANAZone = luxon.IANAZone;
let locations = [
{ lat: -33.865, lng: 151.209444, name: "Sydney"},
{ lat: 51.507222, lng: -0.1275, name: "London" },
{ lat: 42.7235, lng: -73.6931, name: "New York" },
{ lat: 39.739167, lng: -104.990278, name: "Denver"},
{ lat: 34.05, lng: -118.25, name: "Los Angeles"}
];
locations = locations.map(loc => ( { ...loc, timeZone: tzlookup(loc.lat, loc.lng) }));
let rows = [["Location", "Timezone", "UTC Offset (min)", "Current time"], ...locations.map(({ name, timeZone }) => [name , timeZone, IANAZone.create(timeZone).offset(new Date()) + "", new Date().toLocaleString('sv', { timeZone })])];
rows = rows.map(row => row.map(s => s.padEnd(20)).join(""))
rows.forEach(row => console.log(row))
<script src="https://cdn.jsdelivr.net/npm/tz-lookup@6.1.25/tz.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/1.25.0/luxon.min.js"></script>
It seems like this question might have some useful info for you.
How to get a time zone from a location using latitude and longitude coordinates?