Shopping days till christmas counter jQuery
-
13-09-2019 - |
Question
How to calculate shopping days till christmas counter in jQuery. need to add to website. need quick and dirty. no partiaulcar date it needs to correspond to
needs to ignore weekends of course - or maybe not since its for a website. hmm
cant believe there isnt one on here already.
happy holidays everyone!
Solution
You can easily build a function to get the number of days left until a date:
function daysUntil(year, month, day) {
var now = new Date(),
dateEnd = new Date(year, month - 1, day), // months are zero-based
days = (dateEnd - now) / 1000/60/60/24; // convert milliseconds to days
return Math.round(days);
}
daysUntil(2009, 12, 25); // 19 days!!
OTHER TIPS
I've used this jQuery plugin to display countdowns, it's pretty simple to set up. A usage example is available on this page.
Modified CMS's code a bit:
<html>
<head>
<script language="javascript">
function daysUntil() {
var now = new Date();
var year = now.getFullYear();
var month = '12';
var day = '25';
dateEnd = new Date(year, month - 1, day), // months are zero-based
days = (dateEnd - now) / 1000/60/60/24; // convert milliseconds to days
document.getElementById('xmas').innerHTML = 'Days until Christmas: ' + Math.round(days);
}
</script>
</head>
<body>
<div id="xmas"></div>
<script language="javascript">
daysUntil();
</script>
</body>
</html>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow