Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

186
Views
dayjs - not a function

I'm passing two dayjs date to Vars.date global variables.

var dayjs = require("dayjs") for import
   
Vars.date1 = dayjs(whateverdate1("add", 2)).format('MMM D, YYYY')
Vars.date2 = dayjs(whateverdate2("add", 2)).format('MMM D, YYYY')

const date1 = Vars.date1
const date2 = Vars.date2
let diff = date1.diff(date2, 'month')
console.log('diff' + diff)

error: TypeError: date1.diff is not a function

I am unable to use diff and get methods, always 'not a function'

What did I miss?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Make sure that Day.js supports the require() function. If it is import-based, you need to maybe change the import to that.

So, just try doing the following.

import dayjs from "dayjs";

Links

  • npm Package
  • Official Day.js website
about 4 years ago · Juan Pablo Isaza Report

0

I think you had two problems:

  1. add() should be called like dayjs().add(2, 'month')
  2. Your were trying to call diff() on a string (the output of dayjs().format()) rather than a Dayjs object

Here's how you can create two dayjs objects and find their difference in months:

const dayjs = require("dayjs")
// or:
// import dayjs from "dayjs";

const date1 = dayjs()   // Dayjs object of current month
const date2 = dayjs().add(2, "month") // Dayjs object of 2 months from now

date2.diff(date1, "month") // 2
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!