Created
September 29, 2015 04:43
-
-
Save Morgantheplant/a3bea3a4835442255e02 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<input type="date" id="date"/> | |
<input type="text" id="time" /> | |
<div id="countdown"></div> |
This file contains hidden or 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.getElementById('date').addEventListener('change', function(){ | |
//debugger | |
window.requestAnimationFrame(tick) | |
}) | |
function tick(){ | |
var dateVal = document.getElementById('date').value | |
var timeVal = document.getElementById('time').value | |
var timeAry = dateVal.split("-") | |
var year = timeAry.shift() | |
timeAry.push(year) | |
var timeReformat= timeAry.join("/") + " 09:00 PM" | |
var end = new Date(timeReformat) | |
showRemaining(end) | |
} | |
function showRemaining(endDate) { | |
var end = endDate //|| new Date('9/28/2015 08:25 PM'); | |
var _second = 1000, | |
_minute = _second * 60, | |
_hour = _minute * 60, | |
_day = _hour * 24, | |
_month = _day * 30; | |
var now = new Date(), | |
distance = end - now; | |
if (distance < 0) { | |
document.getElementById('countdown').innerHTML = 'EXPIRED!'; | |
return; | |
} | |
var months = Math.floor(distance / _month), | |
days = Math.floor((distance % _month )/ _day), | |
hours = Math.floor((distance % _day) / _hour), | |
minutes = Math.floor((distance % _hour) / _minute), | |
seconds = Math.floor((distance % _minute) / _second); | |
var countdownString = [months,days,hours,minutes,seconds].join(":"); | |
document.getElementById('countdown').innerHTML = countdownString | |
window.requestAnimationFrame(tick) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment