Countdown in days

Not a lot of code needed, just a small helper function to calculate days difference and then a replace content with whatever you want.

Full sample:

<html>
  <div id="counter" />

  <script type="text/javascript">
    function daysDifference($startDate, $endDate) {
      oneDay = 24*60*60*1000;
      return Math.ceil(($endDate.getTime() - $startDate.getTime()) / oneDay);  
    }

    // 2015/01/01
    $startDate = new Date(2015, 0, 1);
    // 2015/12/31
    $endDate = new Date(2015, 11, 31);
    $today = new Date();
    document.getElementById("counter").innerHTML = 'Day ' + daysDifference($startDate, $today) + ': ' + daysDifference($today, $endDate) + ' days left';
  </script>
</html>

http://codepen.io/anon/pen/KwjvqO?editors=100

Leave a Comment