dayjs icon indicating copy to clipboard operation
dayjs copied to clipboard

Cannot set property '$timezone' of undefined

Open herenickname opened this issue 4 years ago • 7 comments

Exception:

node:internal/process/promises:246
          triggerUncaughtException(err, true /* fromPromise */);
          ^

TypeError: Cannot set property '$timezone' of undefined
    at M.f.tz (/root/v3/node_modules/dayjs/plugin/timezone.js:1:1271)
    at Function.withdrawalCompleted (/root/v3/dist/core/common/utils/TelegramNotification.js:31:54)
    at EventEmitter.<anonymous> (/root/v3/dist/core/withdrawal/services/status.service.js:124:63)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

Code (withdrawalCompleted):

import * as DayJS from 'dayjs'
import * as DayJSTimezone from 'dayjs/plugin/timezone'

DayJS.extend(DayJSTimezone)

export class Notification {
     public static async withdrawalCompleted(withdrawal) {
        const dateText = DayJS(withdrawal.createdAt).tz('Europe/Moscow').format('dd.LL.yyyy HH:mm:ss ZZZZ')
        ...
    }
}

Details:

node v16.5.0
npm v7.19.1
dayjs v1.10.6

herenickname avatar Jul 19 '21 00:07 herenickname

Do you use Typescript ? If yes. Enable esmodule interop

// tsconfig.json
{
    "extends": "fastify-tsconfig",
    "compilerOptions": {
        "outDir": "dist",
        "esModuleInterop": true
    },
    "include": ["src/**/*.ts"]
}

and just use it like this

import DayJS from 'dayjs'
import DayJSTimezone from 'dayjs/plugin/timezone'

DayJS.extend(DayJSTimezone)

export class Notification {
     public static async withdrawalCompleted(withdrawal) {
        const dateText = DayJS(withdrawal.createdAt).tz('Europe/Moscow').format('dd.LL.yyyy HH:mm:ss ZZZZ')
        ...
    }
}

mandaputtra avatar Jul 27 '21 16:07 mandaputtra

Having the same problem here, even with esModuleInterop: true

mngu avatar Aug 09 '21 10:08 mngu

Ok, Timezone plugin depends on Utc, don't forget to add it as well !

import DayJS from 'dayjs'
import DayJSUtc from 'dayjs/plugin/utc'
import DayJSTimezone from 'dayjs/plugin/timezone'

DayJS.extend(DayJSUtc)
DayJS.extend(DayJSTimezone)

mngu avatar Aug 09 '21 10:08 mngu

Same here -

import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";

dayjs.extend(timezone);
dayjs.extend(utc);

export const formatTimestamp = (time) => {
  if (!time) {
    return "";
  }
  const tz = dayjs.tz(Number(time) * 1000, "America/New_York");
  return tz.format("MM/DD/YYYY HH:mm");
};

Throws with Uncaught TypeError: Cannot set property '$timezone' of undefined

jonluca avatar Aug 11 '21 19:08 jonluca

First of all, please extend UTC plugin first

Ok, Timezone plugin depends on Utc, don't forget to add it as well!

Second, if you still facing some trouble, feel free to reply here with a demo. (https://npm.runkit.com/dayjs)

iamkun avatar Aug 12 '21 02:08 iamkun

This may happen when you try to access the time zone without initializing it:

// ❌ Wrong, don't do this!
dayjs("23:05", "HH:mm").format("hh:mm z")
// Uncaught TypeError: Cannot read properties of undefined (reading '$timezone')

// ✅ Correct, do this
dayjs.tz("23:05", "HH:mm", "America/New_York").format("hh:mm z") // '11:05 EDT'

I think it would be more intuitive if dayjs used the implied time zone, but for now, that's how it works.

alex996 avatar Sep 12 '22 14:09 alex996