Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

323
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda