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?
CodePudding user response:
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)