1

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.

lost9123193
  • 10,460
  • 26
  • 73
  • 113

2 Answers2

1

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>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    what does padEnd(20) do or where can I read up on this? – lost9123193 Nov 13 '20 at 17:47
  • It will pad the string up to 20 chars, using space as.the default padding char. The docs are here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padEnd – Terry Lennox Nov 13 '20 at 17:50
  • 1
    just updated my question for the method in question I want to apply this to, would it make sense to do this: new Date(endDate).toLocaleString('sv', { timeZone } - new Date(startDate).toLocaleString('sv', { timeZone } ? – lost9123193 Nov 13 '20 at 17:55
  • You could do something like that. Are you looking for the range of timezones for the locations? – Terry Lennox Nov 13 '20 at 18:00
  • 1
    yeah i have different locations at different time zones so when i calculate the range, some will have an extra/less hour while others don't. I'm going to test this out rn thanks! – lost9123193 Nov 13 '20 at 18:03
  • 1
    so turns out it doesn't work. It doesn't convert the object (only the string) :/ – lost9123193 Nov 13 '20 at 19:49
  • You could try to pass the string to the Date constructor, e.g new Date(localTimeString). Then do a getTime() and subtract. – Terry Lennox Nov 13 '20 at 19:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/224542/discussion-between-lost9123193-and-terry-lennox). – lost9123193 Nov 13 '20 at 20:02
  • 1
    @TerryLennox - Don't pass the string output of `toLocaleString` into the `Date` constructor. The `Date` constructor will interpret that as *local time* - not the the time zone you intended. Ultimately the `Date` object cannot represent other time zones. You'll need a library such as [Luxon](https://moment.github.io/luxon/). – Matt Johnson-Pint Nov 13 '20 at 21:21
  • 1
    And I must say I'm impressed with Luxon, it's a nice step on from moment. – Terry Lennox Nov 13 '20 at 21:39
0

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?

pixelearth
  • 13,674
  • 10
  • 62
  • 110