Wrong node.js Date() function date

Problem description:
When the following code is run, the console log date shows that the Date() function returns a wrong date.

let date = new Date()
let month = String(date.getMonth())
let day = String(date.getDate())
console.log(month + "/" + day)

Actual behavior:
The console log returns the wrong date. For example, as of today(3/23), the console returns “2/24”.

Steps to reproduce:
Paste the above code into a replit, then run. I used node.js in the browser website.

Hey @AdamChao1, welcome to the community!

So there’s this thing called time zones. Basically, where you (and I) live, it’s the 23rd of March. But for people on the other side of the world, it’s the 24th! Crazy, right?

1 Like

So how come it returns a date that is a month behind the correct date.

2 Likes

@QwertyQwerty88 That would explain it, except the month is off too.

1 Like

@Sky @Firepup650 oh my gosh I’m blind lmao, maybe I should read the post before replying

1 Like

Yep, to add on to that, generally I would keep dates in Date.now() format and use new Date(date).toLocaleDateString() on the client side to get the correct date. :smile:

2 Likes

It could be that somehow the date/time on Replit’s virtual machines has been configured incorrectly somehow…?

1 Like

Good idea. @AdamChao1 Try typing date into Shell and looking at the output.

yeah, it’s the 24th for @MattDESTROYER

I’m pretty sure JS months are zero indexed. (So 0 is January, 1 is February, 2, is March, etc)

1 Like

I didn’t realize that…
Apparently, that was my problem the whole time. The problem is fixed for me now, thanks.

1 Like

If your problem is solved, please mark the post that solved it as “solution”.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.