0

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) {} 
8
  • 2
    new Date gives you a date according to browser time who in turn asks your OS for the current time. Have you double checked your date & time settings on your computer? Commented May 9, 2019 at 18:06
  • 1
    yes my date and time in the computer is todays date and time. im sorry to not be able to provide mode info but that is all i have unfortunately . edit : its creating the right date in another class , but i do not know why its creating wrong here. Commented May 9, 2019 at 18:07
  • 1
    Are you running this locally or on an online sandbox? Commented May 9, 2019 at 18:09
  • 1
    running locally. edit this is the code :var self = this; var curr = new Date(); var first = curr.getDate() - curr.getDay(); and curr is getting this value Sat May 11 2019 14:06:48 GMT-0400 (Eastern Daylight Time) {} as opposed to todays Commented May 9, 2019 at 18:10
  • 1
    Just tested your code at repl.it/@mikaelbrenner/TrustyModestBoards and it's working fine. Commented May 9, 2019 at 18:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

in my cosole while debugging when i do this : new Date() im getting it right i m just running on my local machine from where im commenting this im not its possible for me to run elsewhere
however i do realize when i hover over the file its giving like this as opposed to local : 5000 : webpack:///. not sure why its the first time im noticing this
0

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

2 Comments

i am going to try this in a moment. thank you for the suggestion
@LuskLuther sure, you're welcome. Hope it works fine!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.