[doc] Timezone example doesn't work

See original GitHub issue

The example code for using timezones in date-fns 2.x doesn’t work:

> npm install date-fns date-fns-timezone
const { zonedTimeToUtc, utcToZonedTime, format } = require('date-fns-tz')

// Set the date to "2018-09-01T16:01:36.386Z"
const utcDate = zonedTimeToUtc('2018-09-01 18:01:36.386', 'Europe/Berlin')

// Obtain a Date instance that will render the equivalent Berlin time for the UTC date 
const date = new Date('2018-09-01Z16:01:36.386Z')
const timeZone = 'Europe/Berlin'
const zonedDate = utcToZonedTime(date, timeZone)
// zonedDate could be used to initialize a date picker or display the formatted local date/time

// Set the output to "1.9.2018 18:01:36.386 GMT+02:00 (CEST)"
const pattern = 'D.M.YYYY HH:mm:ss.SSS [GMT]Z (z)'
const output = format(zonedDate, pattern, { timeZone })
[...]/test.js:4
const utcDate = zonedTimeToUtc('2018-09-01 18:01:36.386', 'Europe/Berlin')
                ^
TypeError: zonedTimeToUtc is not a function 

Edit: I just noticed that the require in the example code above is inconsistent with the demo code, date-fns-tz instead date-fns-timezone. This seems to come from the outdated date-fns documentation.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
rossipediacommented, Sep 1, 2019

date-fns-tz uses the browser’s built-in Intl API for timezone support.

date-fns-timezone relies on timezone-support instead.

0reactions
LucasAndradcommented, Nov 3, 2020

Does this example should also work on terminal using node?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with Time Zones - W3C
Abstract. This document contains guidelines and best practices for working with time and time zones in applications and document formats.
Read more >
alter database set time_zone doesn't work - Ask TOM
One word, it seems the alter database statement doesn't work. Following is a quotes from the document from Metalink: "If the database time...
Read more >
Moment Timezone | Docs
In this example, you first create moment.utc("2013-11-18 11:55") object in UTC, and then change its timezone to specified. This also works if you...
Read more >
Change your doc's time zone - Coda Help Center
If you change your timezone then we will keep the value of the time-based trigger and update it to the new timezone. For...
Read more >
How do I set a timezone in my Jest config? - Stack Overflow
This does not work on windows prior to node 16.2.0 (and prior to 17.0.1 in node 17) - see ... Jest's test DocblockPragmasEnvironment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found