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

utcToZonedTime not accepting the offset #58

Open
psmod2 opened this issue Mar 17, 2020 · 2 comments
Open

utcToZonedTime not accepting the offset #58

psmod2 opened this issue Mar 17, 2020 · 2 comments

Comments

@psmod2
Copy link

psmod2 commented Mar 17, 2020

Hi,

I'm unable to use an offset in the utcToZonedTime().

It works fine with a IANA string like America/Lima - just when I do:

const localTimezoneOffset = new Date().getTimezoneOffset();
const localDate = utcToZonedTime(myUtcDateTime, localTimezoneOffset.toString());

return format(localDate, ("yyyy-mm-dd HH:mm:ss"))
@herrernst
Copy link

I think I have the same use case. I know the offset value of the timezone (in ms, fwiw), but don't have a time zone name. So i just would want to pass that offset as number to utcToZonedTime.

@aerolith07
Copy link

I was having a similar issue and had a look at the documentation.
For a specific hour/mins offset, you need to provide an offset value in the form of [+-]HH or [+-]HHMM, like this:

// PLUS 4 hour offset
const localDate = datefns.utcToZonedTime(new Date(), '+04');

// MINUS 2 hour 30 mins offset
const localDate = datefns.utcToZonedTime(new Date(), '-0230');

@psmod2 If you're using getTimezoneOffset(), it will return an offset in minutes, so you'd need to divide by 60 to get number of hours needed, and then convert to a string which is in the format similar to the example above

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

3 participants