Lusk Luther
Lusk Luther

Reputation: 1

new Date() creating wrong date from typescript class

All I am doing is creating a new Date object with this code:

var currentDate = new Date();

The value I'm getting is:

Sat May 11 2019 13:52:10 GMT-0400 (Eastern Daylight Time) {} 

Upvotes: 0

Views: 2172

Answers (2)

wentjun
wentjun

Reputation: 42516

To avoid all these troubles with timezone, I would recommend to work with UTC instead of your local timezone. UTC is a general time standard and is universal. An ex-colleague of mine showed me this article. It might be relevant!

const utcDate = new Date().toUTCString();
console.log(utcDate);

My recommendation would therefore be, store your time in UTC, but render it as the local time on your application view.

When you need to convert it back to your local timezone (or your user's local client timezone), this is what you can do:

const utcDate = new Date().toUTCString();
const currentDate = new Date(utcDate).toString();
console.log(currentDate);

Upvotes: 0

Fil
Fil

Reputation: 99

press F12 in your browser. in the console write: new Date(); If the date is wrong then its your computer's date which is not set properly. Otherwise like Mikael said, you're running your code on some other machine which has it's date set wrong.

Upvotes: 1

Related Questions