TypeError: dayjs.utc is not a function
Asked Answered
A

3

6

I am getting this error

dayjs.utc is not a function

Here is my code

const dayjs = require('dayjs')

console.log('utc time',dayjs.utc())
Advertising answered 29/12, 2022 at 8:47 Comment(1)
By default, Day.js comes with core code only and no installed plugin. dayjs.gitee.io/docs/en/plugin/pluginMicroreader
A
14

Fix: I tried this it's working:

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

console.log('utc time',dayjs.utc())

Refer docs https://day.js.org/docs/en/plugin/utc

Advertising answered 29/12, 2022 at 8:53 Comment(0)
D
3

If you using import:

import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
dayjs.extend(utc)
Digenesis answered 6/2, 2024 at 12:21 Comment(0)
T
2

This is because of the incorrect method in which you use dayjs. See:

var utc = require('dayjs/plugin/utc')
dayjs.extend(utc)

// default local time
dayjs().format() //2019-03-06T17:11:55+08:00

// UTC mode
dayjs.utc().format() // 2019-03-06T09:11:55Z

// convert local time to UTC time
dayjs().utc().format() // 2019-03-06T09:11:55Z 

// While in UTC mode, all display methods will display in UTC time instead of local time.
// And all getters and setters will internally use the Date#getUTC* and Date#setUTC* methods instead of the Date#get* and Date#set* methods.
dayjs.utc().isUTC() // true
dayjs.utc().local().format() //2019-03-06T17:11:55+08:00
dayjs.utc('2018-01-01', 'YYYY-MM-DD')

Mistake you did:

var utc = require('dayjs')

Fix I shared:

var utc = require('dayjs/plugin/utc')

From : https://day.js.org/docs/en/plugin/utc

Tippet answered 29/12, 2022 at 9:39 Comment(1)
I'm getting this same error with Typescript modules and React. So, I'm using import statements. When I do the same thing I get this error: Cannot use import statement outside a moduleWeisburgh

© 2022 - 2025 — McMap. All rights reserved.