Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!