-2

lol sorry i posted it accidentally

I'm new to JavaScript and i'm trying to make a simple countdown script that should show the difference between the end date and today's server date.

here is a great example of what i'm trying to do http://moblog.bradleyit.com/2009/06/javascripting-to-find-difference.html

The only thing i want to add is another variable with a calculated seconds. How can i do that?

Here is the code:


    var today = new Date();
     var Christmas = new Date("12-25-2009");
     var diffMs = (Christmas - today); // milliseconds between now & Christmas
     var diffDays = Math.round(diffMs / 86400000); // days
     var diffHrs = Math.round((diffMs % 86400000) / 3600000); // hours
     var diffMins = Math.round(((diffMs % 86400000) % 3600000) / 60000); // minutes
     alert(diffDays + " days, " + diffHrs + " hours, " + diffMins + " minutes until Christmas 2009 =)");

Anton Golubev
  • 11
  • 1
  • 2
  • 2
    Ideally, Stack Overflow questions should involve some snippet of code that's giving you trouble (they don't *necessarily* have to, but they often do). You should first attempt to solve your problem and then indicate what specific piece of code is not working how you want. – apsillers Aug 21 '12 at 13:50
  • possible duplicate of [check time difference in javascript](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – epascarello Aug 21 '12 at 13:50
  • 1
    Search stackoverlfow before you asked. There could be 30+ duplicates out there. – epascarello Aug 21 '12 at 13:51
  • Searching google "javascript count down script" would be a good first step. – HandiworkNYC.com Aug 21 '12 at 13:51

3 Answers3

3

You have two issues with this code:

1: You need to use a date that will be accepted across browsers so it needs to be formatted with / instead of -.

2: You are rounding, which when rounding up will give you inaccurate numbers. All numbers need to be rounded down. Here is a function do do so:

var roundDown = function(num){
    var full = num.toString();
    var reg = /([\d]+)/i;
    var res = reg.exec(full);
    return res[1];
}

So your final code should look like this:

var roundDown = function(num){
    var full = num.toString();
    var reg = /([\d]+)/i;
    var res = reg.exec(full);
    return res[1];
}

var today = new Date(); // date and time right now
var goLive = new Date("06/01/2013"); // target date
var diffMs = (goLive - today); // milliseconds between now & target date
var diffDays = roundDown(diffMs / 86400000); // days
var diffHrs = roundDown((diffMs % 86400000) / 3600000); // hours
var diffMins = roundDown(((diffMs % 86400000) % 3600000) / 60000); // minutes
var diffSecs = roundDown((((diffMs % 86400000) % 3600000) % 60000) / 1000 ); // seconds
Soshmo
  • 318
  • 2
  • 7
0
var endDate = new Date(year, month, day, hours, minutes, seconds, milliseconds);
var today = Date.now()
var timeLeft = endDate - today // timeLeft would be in milliseconds
// Parse this into months, days, hours, ...

Put this in a function and set it up to be called every second or so using setInterval.

Gautham Badhrinathan
  • 2,377
  • 3
  • 20
  • 30
0

This should get you started with the JavaScript date object and it's associated methods. http://www.w3schools.com/jsref/jsref_obj_date.asp

Also, look up the setInterval() method, that will allow you to fire code in set intervals (for example, updating the countdown text).