3

I have a little problem with logic, I would like to be able to automatically calculate the difference in days and or months between two dates, ex: there are 20 days left, there is 1 month and 17 days left, ... function that does this directly with dayjs and I can't seem to get the logic of what I want.

Thank you in advance for your help :)

for the moment...

        let creation = dayjs(currentEstimate.createdAt)
        let limit_date = dayjs(currentEstimate.createdAt).add(currentEstimate.estimate_validation_date, 'd')

        let diff = dayjs(limit_date).diff(creation, 'd')

        let months = diff / 31
        if (months < 1){
            console.log(`devis valable ${diff} jours`)
        }else{
            console.log(`devis valable ${months} mois`)
        }

EDIT

Thanks to Stephan Collins ->


let duration = require('dayjs/plugin/duration')
dayjs.extend(duration)
let relativeTime = require('dayjs/plugin/relativeTime')
dayjs.extend(relativeTime)

        dayjs(currentEstimate.createdAt).to(dayjs(currentEstimate.createdAt).add(currentEstimate.estimate_validation_date, 'd'), true)

//one month

Atl31
  • 165
  • 9
  • 2
    This: https://day.js.org/docs/en/durations/diffing, then this: https://day.js.org/docs/en/durations/format or this: https://day.js.org/docs/en/durations/humanize – Stephen Collins Jul 05 '21 at 11:00

2 Answers2

0

I had a similar problem where I wanted to show 3 months and 5 days instead of just 3 months.

The way that I solved it was by creating a custom plugin, but I have modified it into a helper function for this question.

const detailedRelativeTime = (date: Dayjs) => {
  const now = dayjs()
  const duration = dayjs.duration(date.diff(now))

  // These values are whole numbers of each interval format
  const durationInYears = duration.years()
  const durationInMonths = duration.months()
  const durationInDays = duration.days()

  // We don't want to add suffixes to this variable since it will be used as the first part of modified outputs
  const relativeDate = date.fromNow(true)

  // Modify the years output if there is a year with at least 1 month
  // Using !== 0 because values can be negative
  if (durationInYears !== 0 && durationInMonths !== 0) {
    // Add the remaining months from the current date to get the leftover relative time
    // We use add() instead of subtract() because the duration variable can be negative if necessary
    const relativeLeftoverMonths = now.add(durationInMonths, 'months').fromNow()
    // e.g. 1 year and 5 months ago
    return relativeDate + ' and ' + relativeLeftoverMonths
  }

  // Modify the months output if there is a month with at least 1 day
  if (durationInMonths !== 0 && durationInDays !== 0) {
    const relativeLeftoverDays = now.add(durationInDays, 'days').fromNow()
    // e.g. 1 month and 5 days ago
    return relativeDate + ' and ' + relativeLeftoverDays
  }

  // There were no durations that need to be added, so just return the default relative date
  return date.fromNow()
}

You can use this function like so:

detailedRelativeTime(dayjs('2019-01-01')) // 4 years and 4 months ago (at the time of writing)
Eduardo Portet
  • 227
  • 2
  • 7
0

**this max date and min date in pass date **

enter code here

const minDate = new Date("2022-03-03"); const maxDate = new Date("2023-03-03");

let dD = Math.floor(new Date(maxDate - minDate) / (1000 * 60 * 60 * 24 ))

let mD = maxDate.getMonth() - minDate.getMonth()

let myD = maxDate.getYear() - minDate.getYear()

let yD = 0

if(myD > 0){yD=myD*12}

mD += yD console.log(dD)console.log(mD)console.log(myD)