Saturday, 16 February 2019

Count Down time

//= require jquery.countdown for application.js

Download jQuery.countdown.zip

=========================================

Html: haml this code

@aaaaaa.each_with_index do |Object, index|

%p{:id => "counter_#{Object.id}_#{Object.class.name}", class: "counter_time", "data-index": index, "data-id": Object.id, "data-time": (Object.end_date rescue Time.now - 1.days )}


====================================

js:

:javascript

  $(function(){
   
    var date = #{raw (Time.now + 8.days).to_i*1000}

    $(".counter_time").countdown(date, function(event) {

      var time = $(this).attr("data-time");
     
      var countDownDate = moment(time);

      var now = new Date().getTime();
      var time_in_number = new Date(time).getTime();

      var distance = countDownDate - now;

      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);

      if (days < 1)
        days = 0
     
      if (hours < 1)
        hours = 0
     
      if (minutes < 1)
        minutes = 0
     
      if (seconds < 1)
        seconds = 0
     
      var Dcount = "";
      if (days > 1)
        Dcount = " Days | "
      else
        Dcount = " Day | "
     
      var Hcount = ""
      if (hours > 1)
        Hcount = " Hours | "
      else
        Hcount = " Hour | "
     
      var Mcount = ""
      if (minutes > 1)
        Mcount = " Mins | "
      else
        Mcount = " Min | "
     
      var Scount = ""
      if (seconds > 1)
        Scount = " Secs "
      else
        Scount = " Sec "

      if (time === undefined)       
        $(this).html(" ");
      else if (time_in_number < now)
        $(this).html("Already Closed");
      else
        $(this).html(event.strftime(days + Dcount + hours + Hcount + minutes + Mcount + seconds + Scount));

    });
  });