1

I'm trying to learn a little more about JavaScript and decided to make a countdown timer that will show from years all the way down to milliseconds. It's just a learning experiment for me.

The minutes are not correct. If I refresh the browser, the seconds and minutes always start at 59. I think this may be because I am calling the Date object and possibly resetting it. What I am looking for is to count down to a certain date.

Because this is a learning experiment for me, if you see something else that may be improved upon, please let me know.

var dateA = new Date();
var dateB = new Date('June 3, 2014 00:27:00');
var cntr  = setInterval(clock, 10);

function clock()
{
  dateB = (dateB - 10);
  var date = new Date(dateB);
  var yrs  = Math.abs(date.getUTCFullYear() - dateA.getUTCFullYear() );
  var mos  = Math.abs(date.getUTCMonth() - dateA.getUTCMonth());
  var days = Math.abs(date.getUTCDate() - dateA.getUTCDate());
  var hrs  = Math.abs(date.getUTCHours() - dateA.getUTCHours());
  var mins = Math.abs(date.getUTCMinutes() - dateA.getUTCMinutes());
  var secs = Math.ceil(date.getUTCSeconds() - dateA.getUTCSeconds() / 60);
  var mill = Math.ceil(date.getUTCMilliseconds() - dateA.getUTCMilliseconds() / 999);
  var str  =
    yrs  + ' Years   '  +
    mos  + ' Months   ' +
    days + ' Days   '   +
    hrs  + ' Hours   '  +
    mins + ' Mins   '   +
    secs + ' Secs   '   +
    mill + ' Mill';
  document.getElementById('clock').innerHTML = str;
}
3
  • 2
    what is this - dateB = (dateB - 10); Commented Jun 3, 2014 at 3:49
  • That is counting backwards from the original date object. Commented Jun 3, 2014 at 3:56
  • setInterval calls the clock function every 10 milliseconds. In order for the clock function to deincrement, I needed to have dateB = (dateB - 10). Echoing the value of dateB without it does nothing. Commented Jun 3, 2014 at 4:02

3 Answers 3

1
var yrs  = Math.abs(date.getUTCFullYear() - dateA.getUTCFullYear() );
var mos  = Math.abs(date.getUTCMonth() - dateA.getUTCMonth());
var days = Math.abs(date.getUTCDate() - dateA.getUTCDate());
var hrs  = Math.abs(date.getUTCHours() - dateA.getUTCHours());
var mins = Math.abs(date.getUTCMinutes() - dateA.getUTCMinutes());

You cannot just take the absolute value of the differences of each part of the date! You end up with totally wrong numbers.

var secs = Math.ceil(date.getUTCSeconds() - dateA.getUTCSeconds() / 60);
var mill = Math.ceil(date.getUTCMilliseconds() - dateA.getUTCMilliseconds() / 999);

Why would you divide these by 60 and by nearly-1000?!

Instead, to calculate the time difference, you will need to get the complete difference (in milliseconds, usually) and convert that into the different units. Your function should look like this:

var el = document.getElementById('clock');
function clock() {
  var diff = dateB - Date.now();
  var yrs  = Math.floor(diff / 31536000000);
  var mos  = Math.floor(diff / 2678400000) % 12;
  var days = Math.floor(diff / 86400000)   % 31;
  var hrs  = Math.floor(diff / 3600000)    % 24;
  var mins = Math.floor(diff / 60000)      % 60;
  var secs = Math.floor(diff / 1000)       % 60;
  var mill =            diff               % 1000;
  var str  =
    yrs  + ' Years   '  +
    mos  + ' Months   ' +
    days + ' Days   '   +
    hrs  + ' Hours   '  +
    mins + ' Mins   '   +
    secs + ' Secs   '   +
    mill + ' Mill';
  el.innerText = str;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Bergi for the example. Before your edit, I had no idea what you were trying to explain, but with this example, I can clearly see now. Thank you.
Bergi, can you please tell me how you arrived at 2678400000 for the month calculation?
I just approximated a month with 31 days, which are 2678400000 milliseconds. There might be better ideas.
That's fine, thank you. I see what I was doing wrong. Thanks again for putting me on the right track with this snippet.
0

If you're using javascript for comparing dates or counting number of days, you might have some problems. You should use a library for better results.

I recommend you to use http://momentjs.com/ for date or time function. It's easy to use and much more flexible.

This should answer your question: Countdown timer using Moment js

11 Comments

thanks. I am looking at that now. Is there a way to do this using straight JS? Again, this is just a learning experiment for me.
Sure, Moment.js is written in Javascript.
:) I know it's written in JavaScript, but without having yo rely on an external library is what I was asking. Can I do this without the library?
Well, I've done something like this using just straight JS, and it gives me lots of problems, unexpected results and longer codes. Using momentJs I can do the same thing with lesser codes and better results.
All these "Math.abs(date.getUTCFullYear() - dateA.getUTCFullYear() );" gives inaccurate results when comparing.
|
0
try this..

function checkFromDate(sender, args) {
    if (sender._selectedDate > new Date()) {
        alert("You cannot select a day future than today.");
        sender._selectedDate = new Date();
        sender._textbox.set_Value(sender._selectedDate.format(sender._format))
    }
}

1 Comment

Thank you Sandeep. How exactly is this going to help me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.