Skip to content

Instantly share code, notes, and snippets.

@jimmy89Li
Created August 8, 2017 10:24
Show Gist options
  • Select an option

  • Save jimmy89Li/e264a3d1ebbab8d605dc64dbe47d8c52 to your computer and use it in GitHub Desktop.

Select an option

Save jimmy89Li/e264a3d1ebbab8d605dc64dbe47d8c52 to your computer and use it in GitHub Desktop.
<table class="countdown" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td>25-12-2017 12:00</td>
</tr>
</tbody>
</table>
<script>
$('.countdown').each(function(index){
var elm = $(this);
var date = $(this).find('td').html();
var d = date.slice(0, 10).split('-');
var h = date.slice(11, 16).split(':');
var theDays = d[0];
var theMonths = d[1];
var theYears = d[2];
var theHours = h[0];
var theMinutes = h[1];
var theDate = theMonths+'/'+theDays+'/'+theYears+' '+theHours+':'+theMinutes;
console.log(theDate);
var end = new Date(theDate);
var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour * 24;
var timer;
function showRemaining() {
var now = new Date();
var distance = end - now;
if (distance < 0) {
clearInterval(timer);
$('.countdown').html('DONE!');
return;
}
var days = Math.floor(distance / _day);
var hours = Math.floor((distance % _day) / _hour);
var minutes = Math.floor((distance % _hour) / _minute);
var seconds = Math.floor((distance % _minute) / _second);
$('.countdown').html(days + 'D ' + hours + 'H ' + minutes + 'M ' + seconds + 'S ');
}
timer = setInterval(showRemaining, 1000);
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment