I've written an app in Javascript which countdowns to various days (Christmas, Paddy's Day, etc.). I have two problems:
Problem 1:
It works fine on Chrome, but the times are displayed as "NaN" on Safari (iPhone).
I've seen other solutions on OS suggesting to change the date format to include "/" instead of "-" to format the date, but this didn't work for me (or else I'm misunderstanding something).
Problem 2
The countdown does not automatically count down in real time for the paddysDay() function. If I click the button, it updates. The other two functions update automatically every 1000ms. I'm using the exact same setTimeout for all three functions so I can't fathom why it's not working in paddysDay().
Here is the code. Please note that in the xmas() function, I've formatted the date using "/" instead of "-". This format does not work in Chrome OR Safari.
<body>
<fieldset>
<legend><h2>How Many Sleeps Until...?</h2></legend>
<center>
<button onclick = "xmas()">Christmas</button>
<p id="xmas" style="text-align:left;"></p>
<br/>
<button onclick = "myBday()">My Birthday</button>
<p id="myBday" style="text-align:left;"></p>
<br/>
<button onclick = "paddysDay()">Paddy's Day</button>
<p id="paddysDay1" style="text-align:left;"></p>
<p id="paddysDay2" style="text-align:left;"></p>
<br/>
<button onclick = "yourBday()">Your Birthday</button>
<p id="yourBday" style="text-align:left;"></p>
<br/>
</center>
<script>
function xmas() {
var rightNow = new Date();
var currentMonth = (rightNow.getMonth()+1);
var currentDay = rightNow.getDate();
var nextXmasYear = rightNow.getFullYear();
if (currentMonth == 12 && currentDay > 25) {
nextXmasYear++;
};
var nextXmasDate = nextXmasYear + '/12/24T23:59:99.999z';
var xmasDay = new Date(nextXmasDate);
var diffSeconds = Math.floor((xmasDay.getTime()-rightNow.getTime())/1000);
var days = 0;
var hours = 0;
var minutes = 0;
var seconds = 0;
if (currentMonth != 12 || (currentMonth == 12 && currentDay != 25)) {
days = Math.floor(diffSeconds / 86400);
diffSeconds -= days * 86400;
hours = Math.floor(diffSeconds / 3600);
diffSeconds -= hours * 3600;
minutes = Math.floor(diffSeconds / 60);
diffSeconds -= minutes * 60;
seconds = diffSeconds;
}
document.getElementById("xmas").innerHTML = "You've got " + days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds until Christmas.";
setTimeout(xmas, 1000);
}
function myBday() {
var rightNow = new Date();
var currentMonth = (rightNow.getMonth()+1);
var currentDay = rightNow.getDate();
var nextBdayYear = rightNow.getFullYear();
if (currentMonth > 7 || currentMonth == 7 && currentDay >= 14) {
nextBdayYear++;
};
var nextBdayDate = nextBdayYear + '-07-14T00:00:00.000z';
var bdayDay = new Date(nextBdayDate);
var diffSeconds = Math.floor((bdayDay.getTime()-rightNow.getTime())/1000);
var days = 0;
var hours = 0;
var minutes = 0;
var seconds = 0;
if (currentMonth != 7 || (currentMonth == 7 && currentDay != 14)) {
days = Math.floor(diffSeconds / 86400);
diffSeconds -= days * 86400;
hours = Math.floor(diffSeconds / 3600);
diffSeconds -= hours * 3600;
minutes = Math.floor(diffSeconds / 60);
diffSeconds -= minutes * 60;
seconds = diffSeconds;
}
document.getElementById("myBday").innerHTML = "You've got " + days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds to find me a gift.";
setTimeout(myBday, 1000);
}
function paddysDay() {
var rightNow = new Date();
var currentMonth = (rightNow.getMonth()+1);
var currentDay = rightNow.getDate();
var nextPaddysYear = rightNow.getFullYear();
if (currentMonth > 3 || currentMonth == 3 && currentDay >= 17) {
nextPaddysYear++;
};
var nextPaddysDate = nextPaddysYear + '-03-17T00:00:00.000z';
var paddysDay = new Date(nextPaddysDate);
var diffSeconds = Math.floor((paddysDay.getTime()-rightNow.getTime())/1000);
var days = 0;
var hours = 0;
var minutes = 0;
var seconds = 0;
if (currentMonth != 3 || (currentMonth == 3 && currentDay != 17)) {
days = Math.floor(diffSeconds / 86400);
diffSeconds -= days * 86400;
hours = Math.floor(diffSeconds / 3600);
diffSeconds -= hours * 3600;
minutes = Math.floor(diffSeconds / 60);
diffSeconds -= minutes * 60;
seconds = diffSeconds;
}
document.getElementById("paddysDay1").innerHTML = "You've got " + days + " days, " + hours + " hours, " + minutes + " minutes, and " + seconds + " seconds until ...";
setTimeout(paddysDay, 1000);
}
function yourBday(){
document.getElementById("yourBday").innerHTML = "I don't even know who you are, let alone your birthday. Dear Lord, who do think I am? Mark Zuckerberg?";
return;
}
</script>
</fieldset>
<br/>
<div style="height:500px;">
<iframe src="intro"></iframe>
</div>
</body>
Thank you! This is driving me insane!