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

321
Views
Can you use .format() and .form() with dayjs

I'm working on an Angular 8+ project (so I'm using Typscript) and I have a table with some data and I need to display difference of time between two date for each row. On the project I'm using Dayjs.

At the beginning I was using this function:

calculateDelay(alertDate: string, operationDate: string): string {
    if (alertDate && operationDate) {
      return dayjs(alertDate).from(operationDate, true);
    }
  }

and it returned data like this (2 days - 2 years - one day ....)

but there is a problem, with this code, when there is a 1 (day year or whatever) it's written in text and not with number and I would like number only. (The function should return a string but I want this format 1 year and not one year).

So I tried this

  calculateDelay(alertDate: string, operationDate: string): string {
    if (alertDate && operationDate) {
      const date: string = dayjs(alertDate).from(operationDate, true);
      const [value, unit]: string[] = date.split(" ");
      return isNaN(+value) ? "1 " + unit : value + " " + unit;
    }
  }

(isNaN is from loadash)

It work well but there is a problem... with second the output of the first function is some seconds and so my new code return 1 seconds with an error of grammar (since there is only one second).

I know there is a methode called .format() with day js but I just don't know how to use it properlly since the format isn't really fix (I want the result in month, years, minutes when it could be relevant).

Can some please help me ?

Also I would like to avoid custom parsing and deleting useless s at the end of the string (if possible).

Thanks in advance

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

0

As I investigated the repository, you can use the plugin UpdateLocale and override the default config. There is an example for you. DayJs locale override

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!