0

Hi i want this Javscript function to countdown how much time there is left untill the next day stars but i seem to get a problem when i want to automate it.

If i change this code

var eventDate = new Date();
eventDate.setDate(now.getDate() +1);

To this code it will work, but it is not automatic

var eventDate = new Date(2017, 03, 21);

So how can i make it automatic?

function countDown() {
    var now = new Date();
    var currentTime = now.getTime();
    var eventDate = new Date();
    eventDate.setDate(now.getDate() +1);

    var eventTime = eventDate.getTime();

    var remainingTime = eventTime - currentTime;

    var sekunder = Math.floor(remainingTime / 1000);
    var minutter = Math.floor(sekunder / 60);
    var timer = Math.floor(minutter / 60);

    sekunder %= 60;
    minutter %= 60;
    timer %= 24;

    sekunder = (sekunder < 10) ? "0" + sekunder : sekunder;
    minutter = (minutter < 10) ? "0" + minutter : minutter;
    timer = (timer < 10) ? "0" + timer : timer;

    var test = timer + ":" + minutter + ":" + sekunder;
    document.getElementById("countdownTimer").textContent = test;
    setTimeout(countDown, 1000);
}
countDown();

Also if anyone got the time, how do i make it countdown to my servers midnight time insted of the local computer time as Javascript will do?

John Conde
  • 217,595
  • 99
  • 455
  • 496
ii iml0sto1
  • 1,654
  • 19
  • 37
  • 1
    `now.getDate() +1` Wouldn't this mean tomorrow at this exact time and not _untill the next day stars_ – George Apr 20 '17 at 13:56
  • 3
    similar to [Determine minutes until midnight](http://stackoverflow.com/questions/8583694/determine-minutes-until-midnight) – Patrick Barr Apr 20 '17 at 13:58
  • seems to [work](https://jsbin.com/suwivadava/edit?html,js,output) when using the answer linked by @PatrickBarr – George Apr 20 '17 at 14:02
  • To count down to your server midnight, send an ISO 8601 format string for midnight on your server, create a Date from it on the client and count down to it. There are plenty of questions and answers about that already. – RobG Apr 21 '17 at 01:56

1 Answers1

0

You can create event start date this way

var tomorrow = new Date();

tomorrow.setDate(now.getDate() +1);

var eventDate = new Date(tomorrow.getFullYear(), tomorrow.getMonth(), tomorrow.getDate());

and remove

eventDate.setDate(now.getDate() +1);
genichm
  • 525
  • 7
  • 18
  • How does this fix the OP's issue? If the intention is to set the hours to zero, then `tomorrow.setHours(0,0,0,0)` will do that without creating a new Date. – RobG Apr 21 '17 at 01:53
  • Yes, the target is to set hours to zero – genichm Apr 22 '17 at 06:29