I am using the moment-timezone
to obtain the zones and their respective UTC offset within the Nuxt 3
application. Everything is working as expected but the library returns me with 596 timezones which I feel is too much to use within the application as I am using these timezones to populate the Select dropdown field.
Is there a way I can get only important timezones rather than all the timezones? Because I see that there are few duplication of the timezone offset so I tried to remove the duplications but this makes some of the timezone names to miss out:
Following is the approach I am using:
import moment from "moment-timezone";
const timezones = ref([]);
const getTimezonesWithOffset = () => {
const zones = moment.tz.names();
let offsetTmz = [];
for (let i in zones) {
const offset = `(GMT${moment.tz(zones[i]).format("Z")})`;
const offsetObj = { text: zones[i], value: offset };
offsetTmz.push(offsetObj);
}
timezones.value = offsetTmz.sort();
};
onMounted(async () => {
await getTimezonesWithOffset();
console.log(timezones.value.length);
console.log(JSON.stringify(timezones.value, null, 4));
});
I tried to filter for unique offset:
if (!uniqueOffsets.has(offset)) {
uniqueOffsets.add(offset);
formattedTimezones.push(`${zone} - ${offset}`);
}
But using this approach will miss out on some of the zone names but able to get unique UTC offset. I would like to know if there is a way to obtain only the most used or important timezones rather than all the zones using the moment-timezone
library or is there any better library which i can use for getting only important zones.