I'm trying to make a while loop in javascript, which will countdown to every Wednesday to 17:00 PM CEST. I need to get the output in unixtimestamp. I tried to do this:
// seed date, (ANY past wednesday at 17:00)
var seed = new Date(Date.UTC(2021, 5, 30, 17));
var target;
// miliseconds to the next upcoming wednesday 17:00
function timeToTarget() {
while (seed < new Date()) {
seed.setDate(seed.getDate() + 7)
}
target = seed;
return Math.floor(seed - new Date());
}
But something is incorrect and I don't really know what. It's showing me some random day and time 34 years ago. Output should be next wednesday 12.07.2023 17:00 PM CEST.
It's for discord purposes.
Thanks for possible answers!
Technically, you don't need target
and you shouldn't be subtracting anything from it and no need for Math.floor()
.
// seed date, (ANY past wednesday at 17:00)
var seed = new Date(Date.UTC(2021, 5, 30, 17));
// date of the next upcoming wednesday @17:00
function timeToTarget() {
while (seed < new Date()) {
seed.setDate(seed.getDate() + 7)
}
console.log(seed);
}
timeToTarget();