Good day! Such code for some reason gives NaN in Safari. How to fix?

function returnRoubles(roubles) { if(roubles % 10 == 1 && (roubles == 1 || roubles > 20)) return "Ρ€ΡƒΠ±Π»ΡŒ"; else if((roubles % 10 == 2 || roubles % 10 == 3 || roubles % 10 == 4) && (roubles < 10 || roubles > 20)) return "рубля"; else return "Ρ€ΡƒΠ±Π»Π΅ΠΉ"; } function returnCop(roubles) { if((roubles % 10 == 1) && (roubles == 1 || roubles > 20)) return "ΠΊΠΎΠΏΠ΅ΠΉΠΊΡƒ"; else if((roubles % 10 == 2 || roubles % 10 == 3 || roubles % 10 == 4) && (roubles < 10 || roubles > 20)) return "ΠΊΠΎΠΏΠ΅ΠΉΠΊΠΈ"; else return "ΠΊΠΎΠΏΠ΅Π΅ΠΊ"; } function setText() { if(document.getElementById("cnt").innerHTML == "Π’Ρ‹ Π½Π°ΠΊΠΎΠΏΠΈΠ» ΡƒΠΆΠ΅ 0 Ρ€ΡƒΠ±Π»Π΅ΠΉ 0 ΠΊΠΎΠΏΠ΅Π΅ΠΊ") { var curDate = new Date('<?php echo date("Ymd H:i:s"); ?>'); var startDate = new Date('<?php echo $main['startDate']; ?>'); money = Math.floor(((curDate-startDate)*<?php echo $main['money']; ?>)/(864000)); } else money = money + 1; console.log("money: "+money); document.getElementById("cnt").innerHTML = "Π’Ρ‹ Π½Π°ΠΊΠΎΠΏΠΈΠ» ΡƒΠΆΠ΅ <br>"+Math.floor(money/100)+" "+returnRoubles(Math.floor(money/100))+" "+money%100+" "+returnCop(money%100); showMoney(money); } setText(); console.log("interval: "+<?php echo round(864000/$main['money']); ?>); setInterval(setText,<?php echo round(864000/$main['money']); ?>); 

To the screen: You have already saved up NaN rubles NaN kopecks

 function setText() { if(document.getElementById("cnt").innerHTML == "Π’Ρ‹ Π½Π°ΠΊΠΎΠΏΠΈΠ» ΡƒΠΆΠ΅ 0 Ρ€ΡƒΠ±Π»Π΅ΠΉ 0 ΠΊΠΎΠΏΠ΅Π΅ΠΊ") { var curDate = new Date('2017-08-01 16:30:51'); var startDate = new Date('2017-08-01 16:24:40'); money = Math.floor(((curDate-startDate)*50)/(864000)); } else money = money + 1; console.log("money: "+money); document.getElementById("cnt").innerHTML = "Π’Ρ‹ Π½Π°ΠΊΠΎΠΏΠΈΠ» ΡƒΠΆΠ΅ <br>"+Math.floor(money/100)+" "+returnRoubles(Math.floor(money/100))+" "+money%100+" "+returnCop(money%100); showMoney(money); } setText(); console.log("interval: "+17280); setInterval(setText,17280); 

html:

 <h1 class='ontimer' id='cnt'>Π’Ρ‹ Π½Π°ΠΊΠΎΠΏΠΈΠ» ΡƒΠΆΠ΅ 0 Ρ€ΡƒΠ±Π»Π΅ΠΉ 0 ΠΊΠΎΠΏΠ΅Π΅ΠΊ</h1> 
  • And what do you have in js php doing? :) - Alexander Bragin
  • And it’s better to get used to adhere to some kind of code design standard, for example, the JavaScript Style Guide . - Alexander Bragin
  • @AlexanderBragin It is necessary to insert a variable from php into the code in order to loop correctly - AtCliff_
  • Togli is a question with JS. Paste, please, then the sample JS code is already generated (without PHP). Add: the one in which the error occurs. - Alexander Bragin
  • @AlexanderBragin Added. - AtCliff_

2 answers 2

Problem

The problem is specific to Internet Explorer and Safari.

 var date = new Date("2011-02-07"); console.log(date); 

or

 var date = new Date("2011-02-07T11:05:00"); console.log(date); 

If you have executed the above code snippets in different browsers, you will see that in IE you will receive "NaN", and in Safari you will receive "Invalid Date". However, Firefox, Chrome and Opera display the correct date.

Decision

The problem is that you pass the required date to the Date () object. For some reason, do not ask me why the two browsers mentioned above surprisingly do not support the date format β€œyyyy-mm-dd” and therefore fail. I was not able to make a final list of supported date formats, but I can say that the following formats are definitely supported in all browsers and will be advised to stick to one of them to avoid errors:

You need to replace the date format with the following.

 var d = new Date(2011, 01, 07); // yyyy, mm-1, dd var d = new Date(2011, 01, 07, 11, 05, 00); // yyyy, mm-1, dd, hh, mm, ss var d = new Date("02/07/2011"); // "mm/dd/yyyy" var d = new Date("02/07/2011 11:05:00"); // "mm/dd/yyyy hh:mm:ss" var d = new Date(1297076700000); // milliseconds var d = new Date("Mon Feb 07 2011 11:05:00 GMT"); // ""Day Mon dd yyyy hh:mm:ss GMT/UTC 

Full text in original: JavaScript new Date () Returning NaN in IE or Invalid Date in Safari

  • Thank you for tinkering with me) - AtCliff_
  • @atcliff always, please. In dates, as it turned out, and indeed with JavaScript you need to be very careful - the FIG knows how it will behave in browsers. Constantly need to check and test) - Alexander Bragin

NaN - Not a Number You are trying / not getting a number and are trying to use it as a number. Try typing the values ​​you get with the parseInt function. Most suspicious is $main['money'] perhaps you are using a string value in math functions.

  • In all browsers except Safari, everything looks fine. And in the console it is clear that there are strictly numbers. - AtCliff_
  • @AtCliff_, Safari is now like IE6 recently :) - Visman
  • @Visman So, how is it possible for iPhone to display correctly?)) - AtCliff_