question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.tz is not a function

See original GitHub issue

Describe the bug Trying to use the timezone plugin but getting .tz() is not a function error.

Information

  • 1.9.8
  • React Native (Android)
  • America/Vancouver

Setup

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

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

console.log(dayjs().tz('America/Vancouver'))

Error

[Wed Feb 10 2021 09:51:03.304]  ERROR    TypeError: (0, _dayjs.default)().tz is not a function. (In '(0, _dayjs.default)().tz('America/Vancouver')', '(0, _dayjs.default)().tz' is undefined)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:10
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
paulrobellocommented, Jan 7, 2022

The extend method only extends the class in the scope of the current file. When you then import dayjs again in another file you get the un-extended version. To get around this do something similar to @madhurgarg71. I use something like this in a date-utils.ts file:

import dayjs, { Dayjs } from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
import localizedFormat from 'dayjs/plugin/localizedFormat';
import advancedFormat from 'dayjs/plugin/advancedFormat';
import customParseFormat from 'dayjs/plugin/customParseFormat';
import isBetween from 'dayjs/plugin/isBetween';
import isYesterday from 'dayjs/plugin/isYesterday';
import isToday from 'dayjs/plugin/isToday';
import isTomorrow from 'dayjs/plugin/isTomorrow';
import relativeTime from 'dayjs/plugin/relativeTime';
import 'dayjs/locale/en';

dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.extend(localizedFormat);
dayjs.extend(advancedFormat);
dayjs.extend(customParseFormat);
dayjs.extend(isBetween);
dayjs.extend(isYesterday);
dayjs.extend(isToday);
dayjs.extend(isTomorrow);
dayjs.extend(relativeTime);

dayjs.locale('en');
export {
  dayjs,
  Dayjs,
  utc,
  timezone,
  localizedFormat,
  advancedFormat,
  customParseFormat,
  isBetween,
  isYesterday,
  isToday,
  isTomorrow,
  relativeTime
};

Then everyplace else in my application I use something like:

import { dayjs } from './date-util';
console.log(dayjs.utc(this.selectedDate).format());
1reaction
paulrobellocommented, May 1, 2021

You have to import and extend in every file that you want to use the plug-ins

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeError: moment().tz is not a function - Stack Overflow
When testing using jasmine, I am getting this error. TypeError: moment.tz is not a function.
Read more >
Uncaught TypeError: Moment(...).tz is not a function at ... - GitHub
When I try running moment().tz('America/Chicago').format('M') I get the uncaught error. moment.version "2.19.2" moment() ...
Read more >
TypeError: moment.tz is not a function at new SfTimestamp
TypeError: moment.tz is not a function at new SfTimestamp ... It appears to be an issue with the way sf_timestamp.js is using moment...
Read more >
moment/moment-timezone - Gitter
I am getting error: moment().tz is not a function. I am importing moment using es6 modules, do I need import time zone too?...
Read more >
JavaScript : TypeError: moment().tz is not a function - YouTube
JavaScript : TypeError: moment(). tz is not a function [ Gift : Animated Search Engine : https://bit.ly/AnimSearch ] JavaScript : TypeError: ...
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