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

144
Visualizações
How to calculate difference between saved timestamp and current time

I saved a timestamp in this format .

enter image description here

So what I want to do now is calculate the difference between the current time and the saved timestamap in a JavaScript function. But I have no idea how to do that.

Hope anyone can help.

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

0

When you fetch the document, 'timestamp' field would be of type Timestamp and you can use seconds property and current timestamp to calculate the difference as shown below:

const snap = await getDoc(docRef);
const timeDiff = Date.now() - snap.data()?.time.seconds * 1000;

console.log(`Time Difference: ${timeDiff} ms`)
about 4 years ago · Juan Pablo Isaza Relatório

0

This is actually very easy to achieve. Most important to know is that the function Date.now() returns you the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.

To get the duration between to timestamps is like "end - begin = duration".

Here is a code example:

// collect the first timestamp
const beginTimestamp = Date.now()

// here comes a for loop to consume some time, otherwise you will get 0 milliseconds
for (var i=0; i<10000; i++) {
    "Some kind of string".split("").reverse().join("")
}

// collect the second timestamp
const endTimestamp = Date.now()

// subtract the first from the second timestamp tells you the milliseconds in between of both timestamps
console.log("Duration in milliseconds:", endTimestamp - beginTimestamp)
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