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

161
Visualizações
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 Respostas
Responde à pergunta

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