Problème avec la fonction .toISOString ()

Considérez le code suivant HTML + JavaScript:

<!DOCTYPE html> <html> <body> <p id="demo">Click the button to display a date after changing the hours, minutes, and seconds.</p> <button onclick="myFunction()">Try it</button> <script> function myFunction() { var d = new Date(); d.setHours(0,0,0,0); document.write(d + '<br/>'); document.write('ISO Date '+ d.toISOString() + '<br/>'); //I want it to be 2013-04-17T00:00:00.000Z } </script> </body> </html> 

Sortie :

 Thu Apr 18 2013 00:00:00 GMT+0530 (India Standard Time) ISO Date 2013-04-17T18:30:00.000Z 

Quelqu'un pourrait-il aider à comprendre cette différence en Date & Heure

 var d = new Date(); d.setHours(-12, d.getTimezoneOffset(), 0, 0); //removing the timezone offset and 12 hours console.log(d.toISOString()); //2013-04-17T00:00:00.000Z 

Je ne sais pas, pourquoi auriez-vous besoin d'une date ISO un jour plus tôt, mais dans le cas où il s'agit d'une faute de frappe:

 var d = new Date(); d.setHours(0, -d.getTimezoneOffset(), 0, 0); //removing the timezone offset. console.log(d.toISOString()); //2013-04-18T00:00:00.000Z 
 2013-18-04 00:00:00 GMT+0530 2013-17-04 18:30:00 GMT+0000 

Ce sont les deux timestamps. Le premier a un fuseau horaire, le second est GMT (pas de réglage du fuseau horaire). Si vous prenez le deuxième timestamp et ajoutez de 05:30:00 à 18:30:00 vous recevez minuit du lendemain. Cela correspond au premier timestamp.