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)?
Asked
Active
Viewed 6,229 times
3

Ken
- 1,155
- 2
- 19
- 36
-
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."_ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date – Thomas Sablik May 31 '21 at 22:08
-
1`new Date()` returns a Date object. If you are displaying it in the console, it may be using the equivalent of [*toISOString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) which presents a UTC timestamp rather than [*toString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) which presents a local (i.e. host timezone and offset) timestamp. E.g. see [*Date() different output in VS code vs console*](https://stackoverflow.com/questions/65909450/date-different-output-in-vs-code-vs-console). – RobG Jun 01 '21 at 00: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? https://www.w3schools.com/js/js_dates.asp – Ken Jun 01 '21 at 01:51
-
2@ken—w3schools is not a particularly good reference, use [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) or the [ECMAScript language specification (ECMA-262)](https://262.ecma-international.org/). – RobG Jun 01 '21 at 03: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 completely 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 Jun 01 '21 at 05:15
3 Answers
1
Try this:
new Date().toLocaleString()
for just date:
new Date().toLocaleDateString()
for time only:
new Date().toLocaleTimeString()

Aniket Das
- 367
- 1
- 6
- 17
-
thanks, this does work, but now its a string, and I want to keep it as a date time object so I can do some operations on it – Ken May 31 '21 at 21:28
-
1Your date is fine, trust me. Do your operations first and them display the date. – Robo Robok May 31 '21 at 21:34
-
Code–only answers aren't particularly helpful. A good answer should explain why the OP has their issue and how your code fixes it. Plain [*toString*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toString) would likely be more suitable. – RobG Jun 01 '21 at 00:55
-
Ok, but now my question is when I do new Date().getDate() this gives me the date in my local timezone, if I want the date in UTC should I use getUTCDate() or something? – Ken Jun 01 '21 at 02:07
0
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

HappyCod1337
- 1
- 2
0
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.

Anand Rockzz
- 6,072
- 5
- 64
- 71