Home > database >  TypeError: dayjs.utc is not a function
TypeError: dayjs.utc is not a function

Time:12-29

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())

CodePudding user response:

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

CodePudding user response:

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

  • Related