Skip to content

Instantly share code, notes, and snippets.

@MauroJr
Created June 8, 2017 14:31
Show Gist options
  • Select an option

  • Save MauroJr/a155b38ec0196305eb27b1e86bff7594 to your computer and use it in GitHub Desktop.

Select an option

Save MauroJr/a155b38ec0196305eb27b1e86bff7594 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<!--
Created using JS Bin
http://jsbin.com
Copyright (c) 2017 by anonymous (http://jsbin.com/xemiqelalu/1/edit)
Released under the MIT license: http://jsbin.mit-license.org
-->
<meta name="robots" content="noindex">
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<p>Faltam para o próximo Natal:</p>
<p id="nextCmas"></p>
<script id="jsbin-javascript">
(function () {
var MS_IN_1SEC = 1000,
MS_IN_1MIN = MS_IN_1SEC * 60,
MS_IN_1HR = MS_IN_1MIN * 60,
MS_IN_1DAY = MS_IN_1HR * 24;
var pNextCmas = document.getElementById('nextCmas');
setInterval(function () {
var today = new Date(),
currentYear = today.getFullYear(),
cmas = new Date(currentYear, 11, 25),
totalMsLeft, todayTotalMsLeft, todayMinMsLeft,
daysLeft, hrsLeft, minsLeft, secsLeft;
if (today.getMonth() === 11 && today.getDate() > 24) {
cmas.setFullYear(currentYear + 1)
}
totalMsLeft = cmas.getTime() - Date.now();
daysLeft = Math.floor(totalMsLeft / MS_IN_1DAY);
todayTotalMsLeft = totalMsLeft % MS_IN_1DAY;
hrsLeft = Math.floor(todayTotalMsLeft / MS_IN_1HR);
todayMinMsLeft = todayTotalMsLeft % MS_IN_1HR;
minsLeft = Math.floor(todayMinMsLeft / MS_IN_1MIN);
secsLeft = Math.floor((todayMinMsLeft % MS_IN_1MIN) / MS_IN_1SEC);
pNextCmas.textContent = 'Dias: ' + daysLeft + ' - ' +
padLeft2Zeros(hrsLeft) + ':' + padLeft2Zeros(minsLeft) +
':' + padLeft2Zeros(secsLeft);
}, 100);
function padLeft2Zeros(value) {
return ('00' + value).slice(-2);
}
})();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment