最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does new.Date() differs 1 hour from new.Date().toISOString()? - Stack Overflow

programmeradmin2浏览0评论

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()???

Share Improve this question edited Apr 6, 2015 at 19:10 Kalle Richter 8,76729 gold badges93 silver badges204 bronze badges asked Apr 6, 2015 at 18:20 ZidrepZidrep 571 silver badge6 bronze badges 6
  • 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
 |  Show 1 more ment

2 Answers 2

Reset to default 6

The 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

发布评论

评论列表(0)

  1. 暂无评论