Home > OS >  How to convert jquery countdown function to countup function?
How to convert jquery countdown function to countup function?

Time:08-27

I am working on a portal which is basically using a jquery plugin called countdown i.e. https://github.com/hilios/jQuery.countdown

The piece of code is:

$(document).ready(function(){

$("#countdown").countdown("2022/06/25 10:00:00", function(event) {
  var $this = $(this).html(event.strftime(''
       '%-D Days'
});

How can I make it in reverse? Instead of counting it down to the date, is it possible to count from the date? and display how many days it's been.

Any help would be highly appreciated.

CodePudding user response:

As you have not posted the complete countdown function, I am assuming the one from the link you have pasted (https://github.com/hilios/jQuery.countdown)

If you carefully look at the countdown function here https://github.com/hilios/jQuery.countdown/blob/223789964fd1854e04a60d6eba3fd22d20b1f34f/src/countdown.js#L269 you can easily modify it.

I have had similar requirements in the past and writing my own function was easier than modifying the existing one.

For example, the function:

$(document).ready(function () {
    $(function () {
      var calcNewYear = setInterval(function () {
        var date_started = new Date("2022/06/25"); // your date to count from
        var date_now = new Date(); // current timestamp 
        var one_day = 1000 * 60 * 60 * 24;
        var days = Math.floor((date_now - (date_started)) / one_day); // days from the date
        }
    }
}

Then include it wherever it is needed:

("#countdown").html(days   ' Days');
  • Related