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

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

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 Denunciar

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 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