Search code examples
node.jsmomentjsmoment-timezone

Using moment-timezone in node.js


When trying to use a date from a specific timezone a I got the following error:

const dayInGermany = moment().tz("2020-01-31 13:00", "Europe/Berlin").toISOString();

Moment Timezone has no data for 2020-01-31 13:00. See http://momentjs.com/timezone/docs/#/data-loading/.

The page https://momentjs.com/timezone/docs/#/data-loading/ says:

In Node.js, all the data is preloaded. No additional code is needed for loading data.

How can I add the timezone data to my project?


Solution

  • You made a small error.

    Given

    const moment = require('moment-timezone');

    it should be

    const dayInGermany = moment.tz("2020-01-31 13:00", "Europe/Berlin").toISOString();

    not

    const dayInGermany = moment().tz("2020-01-31 13:00", "Europe/Berlin").toISOString();