I have a string which represents the expiry of an item like so: 2020-10-31T21:30:11
, and I have a function to calculate the amount of days left until this date (below).
However, in IE8 it doesn't work. I think this is because timeEnd
is returning NaN
. Can someone explain why this doesn't work and point me in the right direction?
I have a jsFiddle here.
And here's a snippet of my code:
HTML
<span class="days-left" data-publishend="2020-10-31T21:30:11"></span>
JS
$('.days-left').each(function () {
if ($(this).data("publishend")) {
var timeEnd = new Date($(this).data("publishend")), // returns NaN in IE8
timeNow = new Date(),
oneDay = 24*60*60*1000,
oneHour = 60*60*1000,
oneMin = 60*1000,
daysLeft = Math.floor(Math.abs(timeEnd.getTime() - timeNow.getTime()) / oneDay),
hoursLeft = Math.floor(Math.abs(timeEnd.getTime() - timeNow.getTime()) / oneHour),
minsLeft = Math.floor(Math.abs(timeEnd.getTime() - timeNow.getTime()) / oneMin),
string;
if (daysLeft < 1) {
if (hoursLeft < 1.5) {
string = minsLeft + ' minutes';
} else {
string = hoursLeft + ' hours left';
}
}
if (daysLeft === 1) string = '1 day left';
if (daysLeft > 1) string = daysLeft + ' days left';
$(this).text(string);
}
});