Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

330
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda