admin管理员组文章数量:1300167
I've seen various versions of this question, but none of them answer my needs.
I want to create an ISODate for MongoDB and I'm using Node.js.
In Node, when I do:
console.log(Date());
I get:
Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) <-- This is correct.
When I do:
console.log(new Date());
I get:
2016-09-26T19:17:04.731Z <- This is 4 hours ahead
My understanding of the way to do ISODATE is:
var isodate = new Date().toISOString()
console.log(isodate);
Which yields a time 4 hours ahead of "now".
My system date is correct. I run this one different machines, and I get the same results.
Can someone please explain why I'm getting a discrepancy in time?
I've seen various versions of this question, but none of them answer my needs.
I want to create an ISODate for MongoDB and I'm using Node.js.
In Node, when I do:
console.log(Date());
I get:
Mon Sep 26 2016 15:17:04 GMT-0400 (EDT) <-- This is correct.
When I do:
console.log(new Date());
I get:
2016-09-26T19:17:04.731Z <- This is 4 hours ahead
My understanding of the way to do ISODATE is:
var isodate = new Date().toISOString()
console.log(isodate);
Which yields a time 4 hours ahead of "now".
My system date is correct. I run this one different machines, and I get the same results.
Can someone please explain why I'm getting a discrepancy in time?
Share Improve this question edited Dec 13, 2019 at 18:30 Goran Stoyanov 2,3111 gold badge22 silver badges32 bronze badges asked Sep 26, 2016 at 19:26 user5161995user5161995 2-
4
It's not 4 hours ahead. That
Z
at the end of the string means that it's a UTC time stamp. You're 4 hours behind UTC, but the two dates represent the same point in universal time. – Pointy Commented Sep 26, 2016 at 19:28 - this is probably your local timezone offset – chenchuk Commented Sep 26, 2016 at 19:30
3 Answers
Reset to default 4The difference is that 2016-09-26T19:17:04.731Z
related to GMT0 timezone and Mon Sep 26 2016 15:17:04 GMT-0400 (EDT)
to your local timezone. Both are point to the same time :)
You can read more about data formats and timezones in Wiki
With a basic definition to the difference between Date()
and new Date()
is :
Date() ignores any argument(s) passed to it and is equivalent of
new Date().toISOstring()
new Date(Optional_arguments)
creates antime
type object in JS on which you can perform :getTime()
otherDate.prototype
functions listed on MDN WebsiteDate() is just a string representation of local time.
new Date()
gives you a manipulatable object to fiddle around.
Notice the Z
at the end of 2016-09-26T19:17:04.731Z
?
It stands for Zulu, meaning UTC timezone (which is GMT+000).
As you can see in your original date string, Mon Sep 26 2016 15:17:04 GMT-0400 (EDT)
has a GMT-0400
timezone, which I guess is the local time where you live.
So, in fact there is no problem, just different representations of the same time:
Date()
creates a Local datenew Date()
creates a UTC date
本文标签: JavaScriptdifference between Date()new Date()and ISODateStack Overflow
版权声明:本文标题:JavaScript - difference between Date(), new Date(), and ISODate - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741651067a2390482.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论