Created
February 10, 2025 03:05
-
-
Save foyzulkarim/670a7509caf8c54739b231e3cff33310 to your computer and use it in GitHub Desktop.
This file contains a list of timezones and their corresponding cities. It also includes functions to get the GMT offset and find the closest timezone based on a target hour.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file contains a list of timezones and their corresponding cities. | |
* It also includes functions to get the GMT offset and find the closest timezone based on a target hour. | |
*/ | |
// Define 24 hour timezones and their cities | |
const timezones = [ | |
// Americas (West to East) | |
{ city: "Honolulu", timezone: "Pacific/Honolulu", gmt: "GMT-10" }, | |
{ city: "Anchorage", timezone: "America/Anchorage", gmt: "GMT-9" }, | |
{ city: "Vancouver", timezone: "America/Vancouver", gmt: "GMT-8" }, | |
{ city: "Los Angeles", timezone: "America/Los_Angeles", gmt: "GMT-8" }, | |
{ city: "Phoenix", timezone: "America/Phoenix", gmt: "GMT-7" }, | |
{ city: "Denver", timezone: "America/Denver", gmt: "GMT-7" }, | |
{ city: "Mexico City", timezone: "America/Mexico_City", gmt: "GMT-6" }, | |
{ city: "Chicago", timezone: "America/Chicago", gmt: "GMT-6" }, | |
{ city: "New York", timezone: "America/New_York", gmt: "GMT-5" }, | |
{ city: "Toronto", timezone: "America/Toronto", gmt: "GMT-5" }, | |
{ city: "Santiago", timezone: "America/Santiago", gmt: "GMT-4" }, | |
{ city: "São Paulo", timezone: "America/Sao_Paulo", gmt: "GMT-3" }, | |
// Europe/Africa (West to East) | |
{ city: "Reykjavik", timezone: "Atlantic/Reykjavik", gmt: "GMT+0" }, | |
{ city: "London", timezone: "Europe/London", gmt: "GMT+0" }, | |
{ city: "Paris", timezone: "Europe/Paris", gmt: "GMT+1" }, | |
{ city: "Berlin", timezone: "Europe/Berlin", gmt: "GMT+1" }, | |
{ city: "Cairo", timezone: "Africa/Cairo", gmt: "GMT+2" }, | |
{ city: "Istanbul", timezone: "Europe/Istanbul", gmt: "GMT+3" }, | |
{ city: "Moscow", timezone: "Europe/Moscow", gmt: "GMT+3" }, | |
// Asia/Oceania (West to East) | |
{ city: "Dubai", timezone: "Asia/Dubai", gmt: "GMT+4" }, | |
{ city: "Mumbai", timezone: "Asia/Kolkata", gmt: "GMT+5:30" }, | |
{ city: "Bangkok", timezone: "Asia/Bangkok", gmt: "GMT+7" }, | |
{ city: "Singapore", timezone: "Asia/Singapore", gmt: "GMT+8" }, | |
{ city: "Hong Kong", timezone: "Asia/Hong_Kong", gmt: "GMT+8" }, | |
{ city: "Tokyo", timezone: "Asia/Tokyo", gmt: "GMT+9" }, | |
{ city: "Seoul", timezone: "Asia/Seoul", gmt: "GMT+9" }, | |
{ city: "Sydney", timezone: "Australia/Sydney", gmt: "GMT+11" }, | |
{ city: "Auckland", timezone: "Pacific/Auckland", gmt: "GMT+13" }, | |
]; | |
const getGMTOffset = (timezone) => { | |
const now = new Date(); | |
const options = { timeZone: timezone, timeZoneName: "short" }; | |
const formatter = new Intl.DateTimeFormat("en-US", options); | |
const parts = formatter.formatToParts(now); | |
const timeZoneName = parts.find((part) => part.type === "timeZoneName").value; | |
// Extract the GMT offset from the timeZoneName | |
const gmtOffset = timeZoneName.match(/GMT([+-]\d{1,2}(:\d{2})?)/); | |
return gmtOffset ? gmtOffset[0] : "GMT"; | |
}; | |
const findTimezoneByHour = (targetHour) => { | |
// Get current time | |
const now = new Date(); | |
// Calculate time differences and find closest timezone | |
let smallestDifference = 24; | |
let closestCity = ""; | |
let selectedTimezone = ""; | |
let selectedGmt = ""; | |
timezones.forEach(({ city, timezone, gmt }) => { | |
// Get current hour in this timezone | |
const timeInZone = now.toLocaleString("en-US", { | |
timeZone: timezone, | |
hour: "numeric", | |
hour12: false, | |
minute: "numeric", | |
}); | |
const currentHour = parseInt(timeInZone); | |
// Calculate difference with target hour | |
let difference = Math.abs(currentHour - targetHour); | |
// Handle cases crossing midnight | |
if (difference > 12) { | |
difference = 24 - difference; | |
} | |
// Update closest city if this difference is smaller | |
if (difference < smallestDifference) { | |
smallestDifference = difference; | |
closestCity = city; | |
selectedTimezone = timezone; | |
selectedGmt = getGMTOffset(timezone); | |
} | |
}); | |
return { | |
city: closestCity, | |
hourDifference: smallestDifference, | |
timezone: selectedTimezone, | |
gmt: selectedGmt, | |
}; | |
}; | |
// Example usage: | |
console.log(findTimezoneByHour(14)); // Should return a city where it's currently 2 PM | |
// To test current time in different zones: | |
// const now = new Date(); | |
// timezones.forEach(({ city, timezone, gmt }) => { | |
// const timeInZone = now.toLocaleString("en-US", { | |
// timeZone: timezone, | |
// hour: "numeric", | |
// minute: "numeric", | |
// hour12: true, | |
// }); | |
// console.log(`Current time in ${city}: ${timeInZone} ${gmt}`); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment