Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to get a list of all timeZones? #122

Closed
tar-aldev opened this issue May 13, 2021 · 5 comments
Closed

Ability to get a list of all timeZones? #122

tar-aldev opened this issue May 13, 2021 · 5 comments

Comments

@tar-aldev
Copy link

With this library, is it possible to get a list of all timzeones?

E.g. in your examples there are some like

const result = getTimezoneOffset('Africa/Johannesburg')

instead of putting a string, could I somehow use a variable from the library which corresponds to 'Africa/Johannesburg'?
Use Case:
Let's say there is a select with a list of timezones. Although I could just create array of timezones myself, I would have much more confidence if I could import this list from date-fns-tz; It means that if anything is changed inside the library, this list of timezones would be updated as well. Also it would ensure that it is working fine and is taking expected time zone name.

@cureau
Copy link

cureau commented May 14, 2021

date-fns-tz doesn't store timezones, so I believe you have to store it locally or fetch them via an API.

Since a basic timezone string (e.g., Europe/London) can be used as a method param, you basically need a list of strings.

To get the user's local timezone from the browser API: Intl.DateTimeFormat().resolvedOptions().timeZone (user's local timezone for instance (You can use this directly with date-fns-lib)

Here's a list of timezone strings in the same format: https://stackoverflow.com/a/54500197

You could enrich and order this list fairly easily with something like the following. (You can also search the web for a json with the offsets stored alongside the strings — the downside is that some of these values change over time with laws and regulation (hopefully the strings themselves change less).

import { getTimezoneOffset } from 'date-fns-tz';

let orderedTimes = [];
_.forEach(this.tzStrings2, str => {

// converted from date fns milliseconds
const utcOffset = getTimezoneOffset(str);
const hours = utcOffset / (60 * 60 * 1000);

let adjusted = '';
if (hours > 0) {
	adjusted = '+' + hours;
} else if (hours < 0) {
	adjusted = hours;
}

let ret = {
	label: str + ' (UTC' + adjusted + ')',
	hours: hours,
	value: str
};

orderedTimes.push(ret);

});

@maacpiash
Copy link

Moment.js has this feature: https://momentjs.com/timezone/docs/#/data-loading/getting-zone-names/

Sad to see that date-fns-tz doesn't have this feature. Now I have to add Moment.js to my project only for this purpose!

@marnusw
Copy link
Owner

marnusw commented Jul 29, 2021

date-fns-tz is a wrapper for the Intl API, nothing more.

@augnustin
Copy link

This feels awkward to have to store the TZ list in the code, while either date-fns-tz or the browser itself (in case of using Intl API) should know about it.

This doesn't respect the single source of truth paradigm.

Repository owner deleted a comment from augnustin Mar 28, 2022
@danielmarcano
Copy link

In case this helps anyone who comes to this thread, here's how we can obtain all timezones with the Intl API:

const timeZones = Intl.supportedValuesOf('timeZone');

You can read more about it in the MDN docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants