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

121
Visualizações
Difference between hours using moment

I understand this is a popular question on the internet. But the point is that I need to do something accurately and as I'm not very familiar with working with dates, I needed to ask this question.

My problem is the following, from a timestamp I have to define certain actions. These actions are taken under the following conditions:

Prep - starts about an hour before timestamp;

Run - starts right at timestamp;

Reset - this action is taken if 15 minutes have passed after the timestamp;

Bearing in mind that the timestamp would be the following:

const timestamp = "2021-11-16T09:18:02+0000"

I had the idea to do it this way:

const time = moment(timestamp).diff(moment(), "minutes")

But to be honest I don't know how I can improve this. Can you help? 😔

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

0

No need for moment

this should help you get started

const timestamp = "2021-11-16T09:18:02+0000"

const d = new Date(timestamp)

const prep = new Date(timestamp);
prep.setHours(prep.getHours()-1);// Prep - starts about an hour before timestamp;

// Run - starts right at timestamp;


const resetTime = new Date(timestamp)
// Reset - this action is taken if 15 minutes have passed after the timestamp;
resetTime.setMinutes(resetTime.getMinutes()-15)

console.log(d)
console.log(prep)
console.log(resetTime)

console.log((d.getTime()-prep.getTime())/60000)

about 4 years ago · Juan Pablo Isaza Relatório

0

This is the solution I found suitable using momentjs

const timestamp = "2021-11-16T09:18:02+0000"
const reset = moment(timestamp).add(15, 'minutes').format('LT')
const current = moment(timestamp).format('LT')
const prev = moment(timestamp).subtract(1, 'hour').format('LT')

console.log("Reset:",reset,"\nCurrent",current,"\nPrev",prev)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

Which when logged out

reset 3:03 PM current 2:48 PM prev 1:48 PM

For More Regarding momentjs

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