Home » Jquery » jQuery coundown of days not working in Edge-Exceptionshub

jQuery coundown of days not working in Edge-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I have written a small function to display the number of days left between a date and today. It’s working in all browsers except Edge, where its returning NaN.

I originally thought it could be due to Date.parse, but this doesn’t seem to be an issue in Edge, elsewhere. Can anyone see what the issue could be?

function countdown() {
  var endTime = new Date("23 May 2020 13:00 GMT+01:00");
  endTime = (Date.parse(endTime) / 1000);

  var now = new Date();
  now = (Date.parse(now) / 1000);

  var timeLeft = endTime - now;
  var days = Math.floor(timeLeft / 86400);
  $(".to-go span").html(days);
};
countdown();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="to-go"><span></span></div>

Here is a JS fiddle: https://jsfiddle.net/yg6weqs4/2/

How to&Answer:

The issue in Edge is because it cannot parse the "23 May 2020 13:00 GMT+01:00" string to a date. I’d suggest changing this to an ISO8601 formatted date string, eg:

var endTime = new Date("2020-05-23T13:00:00+01:00");            

Working example