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

javascript - new Date() returns UTC time instead of local time - Stack Overflow

programmeradmin0浏览0评论

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

Share Improve this question asked May 31, 2021 at 21:16 KenKen 1,4814 gold badges23 silver badges45 bronze badges 5
  • 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 a Number 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
Add a ment  | 

3 Answers 3

Reset to default 2

Try 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.

发布评论

评论列表(0)

  1. 暂无评论