Last active
November 15, 2018 17:13
-
-
Save minimaxir/06565c07be832b762c8c673c9ec940a3 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>A simple clock</title> | |
</head> | |
<body translate="no" > | |
<div id="output"></div> | |
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js'></script> | |
<script> | |
// https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript | |
var urlParams; | |
(function () { | |
var match, | |
pl = /\+/g, // Regex for replacing addition symbol with a space | |
search = /([^&=]+)=?([^&]*)/g, | |
decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); }, | |
query = window.location.search.substring(1); | |
urlParams = {}; | |
while (match = search.exec(query)) | |
urlParams[decode(match[1])] = decode(match[2]); | |
})(); | |
var output = document.getElementById("output"); | |
if (urlParams["style"]) output.setAttribute("style", urlParams["style"]); | |
if (urlParams["bodyStyle"]) document.body.setAttribute("style", urlParams["bodyStyle"]); | |
var c; | |
setInterval( | |
c = function() { | |
// https://stackoverflow.com/questions/18623783/get-the-time-difference-between-two-datetimes | |
var now = moment(); | |
var targetTime = urlParams["targetTime"]; | |
var countdown = moment(targetTime).diff(now) | |
if (countdown > 0) { | |
var countdownText = moment.utc(countdown).format("HH:mm:ss"); | |
} | |
else { | |
var countdownText = "Now!" | |
} | |
output.innerText = countdownText; | |
}, 1000); | |
c(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Variant of https://gist.github.com/sam0737/a0ee8ca253fc5c84b2aa2ac018f7b8ad to calculate a countdown between Now and a
targetDate
. (MIT)