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

119
Visualizações
Realtime date time with JavaScript

I want to check the time elapsed from now. For example 5 minutes ago like that. I want to make it live without any page refresh. Is date-fns package is suitable for this? and how to do so, Can I watch for the changes with a Watcher in Vue JS. Thanks if anyone can help me with this situation.

formatDistance method in date-fns is suitable for this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'll assume you are using the composition API.

You can use the useTimeAgo composable from VueUse. It returns a formatted string in the format you described, updating with a specified interval.

Example:

import { useTimeAgo } from '@vueuse/core'

const theEvent = new Date(2022, 0, 1)

// The time can be a Date, string, or number, or a `ref` holding one of those.
// Whatever you give, the value is passed through `Date`.
const timeAgo = useTimeAgo(theEvent, { updateInterval: 30_000 }) // Update every half minute

You cannot specify requestAnimationFrame as the interval, unlike with the underlying useNow composable, but this shouldn't ever be needed. Antfu's response when I asked:

useTimeAgo only needs very low accuracy of the time, using requestAnimationFrame would be too costly and would increase the bundle size.

See also: useNow on VueUse.

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