I'm attempting to display how much time is left, based on a mysql timestamp. For some reason the output is -1 days, 23 hours, 59 minutes, 59 seconds.
<script type="text/javascript">
function update(datetime = "2021-07-15 20:24:42") {
timeleft = new Date(datetime);
now = new Date();
secs = (timeleft - now) / 1000;
days = Math.floor(secs / (3600 * 24));
hours = Math.floor((secs - (days * (3600 * 24)))/3600);
minutes = Math.floor((secs - (days * (3600 * 24)) - (hours * 3600)) / 60);
seconds = Math.floor(secs - (days * (3600 * 24)) - (hours * 3600) - (minutes * 60));
if (seconds < 0) {
days = 0;
hours = 0;
minutes = 0;
seconds = 0;
}
return days+' days, '+ hours+' hours, '+minutes+' minutes, '+seconds+' seconds';
}
// time is pulled from database, but plugged in manually
member = Date("06-08-21 16:06:37");
alert("Left: "+update(member));
</script>
Any info would be appreciated
membersuggest it's the first, anddatetimesuggests it's the later. Neither of these formats are suitable for javascriptDate(), you'll need convert it into proper format first.Datelikenew Date()