JavaScript does not work in Safari / Edge?

2

Recently I asked for guidance here in SO asking for help to make a timer via JavaScript.

Andbyaccessingtheotherbrowsers,itloadsnormally:

MyquestionisisthereanyspecialhandlingforBroser"understand" JavaScript ?

function Cronometro(dtFinal, ciclo) {
   var d, m, a;
   var dtF = dtFinal.replace("/", " ").replace("/", " ");
   dtF = dtF.split(" ");
   d = dtF[0];
   m = dtF[1];
   a = dtF[2];
   var countDownDate = new Date(m+" "+d+","+a).getTime();

   // Update the count down every 1 second
   var x = setInterval(function () {

       // Get todays date and time
       var now = new Date().getTime();

       // Find the distance between now an the count down date
       var distance = countDownDate - now;

       // Time calculations for days, hours, minutes and seconds
       var days = Math.floor(distance / (1000 * 60 * 60 * 24));
       var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
       var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
       var seconds = Math.floor((distance % (1000 * 60)) / 1000);

       // Output the result in an element with id="demo"

       document.getElementById("demo").innerHTML = "Faltam " + days + " Dias " + hours + " Horas "
           + minutes + " Minutos para o fechamento da Semana " + ciclo;

       //If the count down is over, write some text 
       if (distance < 0) {
           clearInterval(x);
           document.getElementById("demo").innerHTML = "";
       }
   }, 1000);

}
        
Cronometro("30/06/2018", "14/2018");
<div id="demo"></div>
    
asked by anonymous 13.06.2018 / 23:00

2 answers

7

Browsers have variations on the date formats they can interpret. The code you linked uses the following:

var countDownDate = new Date(m+" "+d+","+a).getTime();

This format is not very usual and does not follow the established standards (a variation of ISO 8601 extended ). You could say that it worked in some browsers by luck. To correct, I suggest using another form of the constructor Date , passing year, month and day separately, in this order, instead of string.

var countDownDate = new Date(a, m-1, d).getTime();
  

Note
  The MDN documentation states that Date.parse supports the format of dates from RFC2822 (which is about e-mail message format), that is, dates as Aug 12, 2018 . In fact, javascript engines usually accept this format, but it is worth noting that it is not mentioned in the language specification (ECMA-262). The safest thing would be to avoid.

    
13.06.2018 / 23:35
1

These browsers you quoted do not actually recognize the numeric format of the month on the date, type "06 15, 2018" . It would have to be "Jun 15, 2018" .

This documentation indicates that the string format is valid:

Inthiscaseyoucanconvertthenumberforthemonthbytheabbreviationofthemonthname(egJune=Jun).

Youcanuseanarraytogetitsnamebychangingthevalueofm:

m=meses[parseInt(dtF[1])-1];//parseIntpararetiraro"0" do número do mês (06 => 6)
// o -1 para pegar o elemento no índice correto da array

See it working:

Print Edge

PrintSafari

var meses = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

function Cronometro(dtFinal, ciclo) {
   var d, m, a;
   var dtF = dtFinal.replace("/", " ").replace("/", " ");
   dtF = dtF.split(" ");
   d = dtF[0];
   m = meses[parseInt(dtF[1])-1];
   a = dtF[2];

   var countDownDate = new Date(m+" "+d+","+a).getTime();

   // Update the count down every 1 second
   var x = setInterval(function () {

       // Get todays date and time
       var now = new Date().getTime();

       // Find the distance between now an the count down date
       var distance = countDownDate - now;

       // Time calculations for days, hours, minutes and seconds
       var days = Math.floor(distance / (1000 * 60 * 60 * 24));
       var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
       var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
       var seconds = Math.floor((distance % (1000 * 60)) / 1000);
       

       // Output the result in an element with id="demo"

       document.getElementById("demo").innerHTML = "Faltam " + days + " Dias " + hours + " Horas "
           + minutes + " Minutos para o fechamento da Semana " + ciclo;

       //If the count down is over, write some text 
       if (distance < 0) {
           clearInterval(x);
           document.getElementById("demo").innerHTML = "";
       }
   }, 1000);

}
        
Cronometro("15/06/2018", "14/2018");
<div id="demo"></div>
    
13.06.2018 / 23:39