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

156
Vistas
how can I make this date logic more generic? flip string every n [timeunit]

I have the following:

const d = new Date();
const data = `${d.getFullYear()}-${d.getMonth()}-${d.getDay()}-${d.getHours()}-${d.getMinutes()>30?30:0}`

Effectively this gives a new string every 30 min.

I was thinking how to do this by passing a amount and having the same effect for [n] seconds/minutes

I tried to use getTime() but I am stuck so far. That approach gives me a whole number which I found no way to workout the same logic as above.

Any ideas how I can achieve that?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Assuming you don't care about the contents of the string, just want it to be unique within the interval. You can divide timestamp by the interval you want the string to be the same.

function dateKey(intervalMs) {
  return (Date.now() / intervalMs | 0).toString();
}

function dateKeySeconds(intervalSec) {
  return dateKey(intervalSec * 1000)
}

function dateKeyMinutes(intervalMin) {
  return dateKey(intervalMin * 60 * 1000)
}

// This will log new string every 30 seconds
setInterval(() => {
  console.log(dateKeySeconds(30))
}, 10000)

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