admin管理员组文章数量:1355658
I read that new Date()
is supposed to return the date on your systems timezone, but for some reason I get the UTC timezone instead 2021-05-28T04:00:00.000Z
. Why is this happening and how can I get the current time in the local timezone (working on a React Native project with Expo)?
I read that new Date()
is supposed to return the date on your systems timezone, but for some reason I get the UTC timezone instead 2021-05-28T04:00:00.000Z
. Why is this happening and how can I get the current time in the local timezone (working on a React Native project with Expo)?
-
2
"I read that
new Date()
is supposed to return the date on your systems timezone" Where have you read it? That's wrong. "JavaScript Date objects represent a single moment in time in a platform-independent format.Date
objects contain aNumber
that represents milliseconds since 1 January 1970 UTC." developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – Thomas Sablik Commented May 31, 2021 at 22:08 -
2
new Date()
returns a Date object. If you are displaying it in the console, it may be using the equivalent of toISOString which presents a UTC timestamp rather than toString which presents a local (i.e. host timezone and offset) timestamp. E.g. see Date() different output in VS code vs console. – RobG Commented Jun 1, 2021 at 0:54 - This is one of the places that mentions this but maybe I've gotten something mixed up? Could you help me understand this better? w3schools./js/js_dates.asp – Ken Commented Jun 1, 2021 at 1:51
- 2 @ken—w3schools is not a particularly good reference, use MDN or the ECMAScript language specification (ECMA-262). – RobG Commented Jun 1, 2021 at 3:12
-
1
@Ken as mentioned W3Schools is not considered a good source. This is one of the instances where they are downright wrong and misleading. "By default, JavaScript will use the browser's time zone and display a date as a full text string:" this is pletely incorrect. There is no "default" display of dates. Not one shared by all browsers and environments. When displaying a date you should at the very least use
.toLocaleString()
or.toISOString()
for formatting. Or make an even more custom format using the date object. You shouldn't rely on defaults. – VLAZ Commented Jun 1, 2021 at 5:15
3 Answers
Reset to default 2Try this:
new Date().toLocaleString()
for just date:
new Date().toLocaleDateString()
for time only:
new Date().toLocaleTimeString()
I got around it like this:
const d = new Date();
const month = d.getMonth() + 1; // it returns 0 for January and 11 for December
const day = d.getDate();
const year = d.getFullYear();
const hours = d.getHours() + 1; // for UTC+1 ,
const minutes = "0" + d.getMinutes();
const seconds = "0" + d.getSeconds();
const formattedTime = day + '.' + month + '.' + year + ', ' + hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);
console.log(formattedTIme);
// Output: 2.12.2021, 16:47:47
I had the same issue in the code, for example:
const c = {d1 : new Date(1672720648000)}
console.log(c.d1.toString()) //Mon Jan 02 2023 20:37:28 GMT-0800 (Pacific Standard Time)
console.log(JSON.stringify(c))//{"dd":"2023-01-03T04:37:28.000Z"}`
c.d1 = c.d1.toString() // -> You can fix this issue by storing string in your json
console.log(JSON.stringify(c))//{"d1":"Mon Jan 02 2023 20:37:28 GMT-0800 (Pacific Standard Time)"}
So I realized the issue is in the way JSON.stringify works.
The issue was NOT with Date or react.
本文标签: javascriptnew Date() returns UTC time instead of local timeStack Overflow
版权声明:本文标题:javascript - new Date() returns UTC time instead of local time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744010803a2575529.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论