JSFiddle - React, Tailwind, and code Playground
by Mick Harner
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<span class="demo" data-val="2017-03-25 02:03:04"></span><br>
<span class="demo" data-val="2017-03-15 11:01:13"></span>
JavaScript
$(".demo").each(function(index){
var obj=$(this);
var countDownDate=new Date(obj.data('val')).getTime();
var x=setInterval(function(obj,countDownDate){
var now=new Date().getTime();
var distance=countDownDate-now;
if (distance<0){
clearInterval(x);
obj.text("EXPIRED");
}else{
// 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);
obj.text(days+"d "+hours+"h "+minutes+"m "+seconds+"s ");
}
}, 1000,obj,countDownDate);
});