javascriptnode.jstimezonedayjs

TypeError: dayjs(...).tz is not a function


I am getting this error.

TypeError: dayjs(...).tz is not a function

My code is

const dayjs = require('dayjs');

const dayjsAmerica = dayjs("2014-06-01 12:00").tz("America/New_York");


Solution

  • You need to extend the dayjs class with the utc and timezone classes/plugins:

    const dayjs = require('dayjs');
    const utc = require('dayjs/plugin/utc');
    const timezone = require('dayjs/plugin/timezone');
    dayjs.extend(utc);
    dayjs.extend(timezone);
    

    Refer to docs for more details: https://day.js.org/docs/en/timezone/timezone