0

I want to count down to a specific time on a specific date. All I have now is count to a specific date, which is at 12:00am. I need to count it down to something like 3:00pm. Is this possible?

What I got now:

var date = new Date(2015, 6, 31); // !!! Maanden beginnen bij 0, dus Januari is 0.
var now = new Date();
var diff = (date.getTime()/1000) - (now.getTime()/1000);

Thanks for the help!

EDIT:

Apparently it was much simpler than I thought ...

I just had to do

var date = new Date(2015,6, 31, 15,00);
Rubenxfd
  • 434
  • 1
  • 5
  • 19
  • could you please explain this in more detail or a better way. – Robin Jul 16 '15 at 14:53
  • Hey Robin, I would like to trigger a function on a specific time on a specific date. Let's say August 22nd at 3pm. So not only the date, but also the time is important. Hope this is more clear. – Rubenxfd Jul 16 '15 at 15:12
  • 1
    see if this works for you http://stackoverflow.com/questions/19088040/how-can-i-run-a-function-at-specific-time-date – Robin Jul 16 '15 at 15:16

1 Answers1

0

Do you mean by counting the seconds? I assume you get the seconds for the difference between the two dates. An example should be like this:

var date = new Date(2015, 6, 31); // !!! Maanden beginnen bij 0, dus Januari is 0.
var now = new Date();
var diff = (date.getTime()/1000) - (now.getTime()/1000);
var count=diff;
var counter=setInterval(timer, 1000); //1000 will  run it every 1 second

function timer()
{
  count=count-1;
  if (count <= 0)
  {
     clearInterval(counter);
     return;
  }

 document.getElementById("timer").innerHTML=count + " secs"; // watch for spelling
}

You can show the time like this:

<span id="timer"></span>

I hope it can help in what you want to do.

Example in JsFiddle

Nelson Parra.

Nelson Parra
  • 101
  • 1
  • 5