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

136
Views
Days between dates in DayJS UK Format

Im using dayjs, and I'm trying to calculate the days between 2 days so that I can output Today, Tomorrow and X days time

The problem I'm having though seems to be with the date formats

const date1 = dayjs('31/10/2021');
const date2 = dayjs().format("DD/MM/YYYY")

let hours = date2.diff(date1, 'days')

No matter how I try, I'm getting date2.diff is not a function when I add .format to date2. Does anyone know how to format this correctly and get the correct outcome?

If i try const date1 = dayjs('31/10/2021').format("DD/MM/YYYY"); then it errors with invalid date

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

0

You can try the following:

const date1 = dayjs('2021-01-25');
const date2 = '01/11/2020'; // or '2020-11-01'

let hours = date1.diff(date2, 'day')

console.log(hours) // 380

The format function just returns a string. This is why date2.diff is not a function. The diff function is only available for dayjs objects (not for strings).

Docs for difference function: https://day.js.org/docs/en/display/difference

Docs for format function: https://day.js.org/docs/en/display/format

about 4 years ago · Juan Pablo Isaza Report

0

const date1 = dayjs('2019-01-25')
date1.diff('2018-06-05', 'day', true)

You don't need to call format function, just use dayjs constructor to convert both dates to dayjs object and return the difference between the 2 dates in number of days.

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!