admin管理员组文章数量:1321251
Please someone explain me this situation.
I have the following code:
<p>Click the button to display the date and time as a string, using the ISO standard.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo1"></p>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
var n = d.toISOString();
document.getElementById("demo1").innerHTML = d;
document.getElementById("demo").innerHTML = n;
}
</script>
And I get the following result:
Click the button to display the date and time as a string, using the ISO standard.
Try it
Mon Apr 06 2015 19:07:55 GMT+0100 (GMT Daylight Time)
2015-04-06T18:07:55.739Z
Why does the toISOString()
method "take" 1 hour away from new Date()
???
Please someone explain me this situation.
I have the following code:
<p>Click the button to display the date and time as a string, using the ISO standard.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo1"></p>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date();
var n = d.toISOString();
document.getElementById("demo1").innerHTML = d;
document.getElementById("demo").innerHTML = n;
}
</script>
And I get the following result:
Click the button to display the date and time as a string, using the ISO standard.
Try it
Mon Apr 06 2015 19:07:55 GMT+0100 (GMT Daylight Time)
2015-04-06T18:07:55.739Z
Why does the toISOString()
method "take" 1 hour away from new Date()
???
-
2
The answer is right under your eyes:
GMT+0100
– blex Commented Apr 6, 2015 at 18:22 - 2 Are you asking why you live in a timezone that is one hour ahead of GMT, and why GMT time isn't the same as the time where you live ? – adeneo Commented Apr 6, 2015 at 18:22
- @blex I see that! But I also see hour=19. – Zidrep Commented Apr 6, 2015 at 18:28
- @adeneo that's not what I'm asking. new Date() gives Mon Apr 06 2015 19:07:55 GMT+0100 (GMT Daylight Time). Why when using toISOString method hour is 18 and not 19? – Zidrep Commented Apr 6, 2015 at 18:29
- Well, where you live, it's 7pm (19), because your timezone is 1 hour ahead from the GMT time. So, the actual GMT time is 6pm (18). Does that make sense? – blex Commented Apr 6, 2015 at 18:33
2 Answers
Reset to default 6The trailing Z(because of which you are facing the difference) which represents Zulu timezone. Your actual time is perhaps 1 hour ahead of the GMT time. And if you want to get rid of the difference because of that you can try this:
var x = (new Date()).getTimezoneOffset() * 60000;
var localISOTime = (new Date(Date.now() - x)).toISOString().slice(0,-1);
On a side note:
moment.js
is good option to choose to get rid of these issues.
The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ. The timezone is always zero UTC offset, as denoted by the suffix "Z".
(Emphasis mine)
See MDN
本文标签: javascriptWhy does newDate() differs 1 hour from newDate()toISOString()Stack Overflow
版权声明:本文标题:javascript - Why does new.Date() differs 1 hour from new.Date().toISOString()? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742096271a2420567.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论