Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JS countdown to specific day and time in while loop

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks for possible answers!

>Solution :

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();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading