.toISOString()函数问题 [英] Issue with .toISOString() function

查看:106
本文介绍了.toISOString()函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码HTML + JavaScript:

Consider the following code 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>

输出

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

任何人都可以帮助理解日期和时间的差异。时间

Could anyone help on understanding this difference in Date & Time

推荐答案

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

我不知道,你为什么要这样做?需要提前一天提交ISO日期,但如果是拼写错误:

I don't know, why would you need a ISO date a day earlier, but in case it is a typo:

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

这篇关于.toISOString()函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆