Jump to content
Sign in to follow this  
DesignMonkey

Help me make a countup timer (date)

Recommended Posts

Hey everyone! I could really use some help making my "countdown" timer count up by days, minutes, seconds. How do I modify the code below to accomplish this! Thanks again! 

 

<!-- Display the countdown timer in an element -->
<p id="demo"></p>

<script>
// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = countDownDate - now;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

 

Share this post


Link to post
Share on other sites

I don't know what you're trying to do.  Are you trying to show how much time has elapsed from a date in the past instead of how much there is until a date in the future?

Share this post


Link to post
Share on other sites

This one should be pretty simple, this should count up. Its just as simple as making sure that the 'countDownDate' is set to something in the past.

 

All you need to do is replace two bits

// Set the date we're counting down to
var countDownDate = new Date("Jan 5, 2021 15:37:25").getTime();

With

// Set the date we're counting down to
var countDownDate = new Date().getTime();

 

And then this

var distance = countDownDate - now;

with

var distance = now - countDownDate;

 

Edited by Funce

Share this post


Link to post
Share on other sites

Thank you everyone for all of your help, but this doesn't quite solve my issue. I was a little vague about what I wanted this timer to accomplish I suppose.

On my companies website I show the years of experience with a big heading that says "29". Underneath that heading I have some text that says "Years of experience and counting".

I'm trying to get the timer to count up from Aug 21 of 2018 to Aug 21 of 2019 to show that with every passing day our company is gaining experience and growing. 

So it would look something like:

29

Years of experience and counting

Days, Hours, Minutes, Seconds <- and then the program expires once the timer hits 365 Days.

Edited by DesignMonkey

Share this post


Link to post
Share on other sites

I would start the counter at the original date  29 years ago instead of 2018.  Then you can use the code above.  You can get the years and display that in one place, and display the rest of the info somewhere else if you want.

Share this post


Link to post
Share on other sites

Ladies and Gentleman, I have solved my own issue.

Please reference this code below if you're ever trying to count up from one date to another. 
 

<script>
// Set the date we're counting down to
var countStartDate = new Date("Aug 21, 2018").getTime();
var countYearStart = new Date ("Jan 1, 2019").getTime();


// Update the count down every 1 second
var x = setInterval(function() {

  // Get todays date and time
  var now = new Date().getTime();

  // Find the distance between now and the count down date
  var distance = now - countYearStart + countYearStart - countStartDate ;

  // Time calculations for days, hours, minutes and seconds
  var days = Math.floor(distance / (1000 * 60 * 60 * 24));
  var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
  var seconds = Math.floor((distance % (1000 * 60)) / 1000);

  // Display the result in the element with id="demo"
  document.getElementById("demo").innerHTML = days + "d " + hours + "h "
  + minutes + "m " + seconds + "s ";

  // If the count down is finished, write some text 
  if (distance < 0) {
    clearInterval(x);
    document.getElementById("demo").innerHTML = "EXPIRED";
  }
}, 1000);
</script>

Cheers

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...