Created
November 1, 2012 04:31
-
-
Save dleatherman/3991746 to your computer and use it in GitHub Desktop.
A simple javascript countdown clock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).ready(function(){ | |
countdown(); | |
setInterval(countdown, 1000); | |
function countdown () { | |
var now = moment(), // get the current moment | |
// May 28, 2013 @ 12:00AM | |
then = moment([2013, 4, 28]), | |
// get the difference from now to then in ms | |
ms = then.diff(now, 'milliseconds', true); | |
// If you need years, uncomment this line and make sure you add it to the concatonated phrase | |
/* | |
years = Math.floor(moment.duration(ms).asYears()); | |
then = then.subtract('years', years); | |
*/ | |
// update the duration in ms | |
ms = then.diff(now, 'milliseconds', true); | |
// get the duration as months and round down | |
months = Math.floor(moment.duration(ms).asMonths()); | |
// subtract months from the original moment (not sure why I had to offset by 1 day) | |
then = then.subtract('months', months).subtract('days', 1); | |
// update the duration in ms | |
ms = then.diff(now, 'milliseconds', true); | |
days = Math.floor(moment.duration(ms).asDays()); | |
then = then.subtract('days', days); | |
// update the duration in ms | |
ms = then.diff(now, 'milliseconds', true); | |
hours = Math.floor(moment.duration(ms).asHours()); | |
then = then.subtract('hours', hours); | |
// update the duration in ms | |
ms = then.diff(now, 'milliseconds', true); | |
minutes = Math.floor(moment.duration(ms).asMinutes()); | |
then = then.subtract('minutes', minutes); | |
// update the duration in ms | |
ms = then.diff(now, 'milliseconds', true); | |
seconds = Math.floor(moment.duration(ms).asSeconds()); | |
// concatonate the variables | |
diff = '<span class="num">' + months + '</span> months<br><span class="num">' + days + '</span> days<br><span class="num">' + hours + '</span> hours<br><span class="num">' + minutes + '</span> minutes<br><span class="num">' + seconds + '</span> seconds…'; | |
$('#relative').html(diff); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
apparently on moment.js v2.8.1
moment().subtract(period, number)
was deprecated and the api was changed tomoment().subtract(number, period)
just a switch in args will do the trick. Thank you!----~https://gist.github.com/paparts/71d068d132ee2f7d5883