This is a common question asked by many people in areas with negative GMT, so to understand better, I will explain more in the next part of the article, showing you how to avoid the error getDate() returns the wrong date in JavaScript anymore.
Why does the error getDate() returns the wrong date in JavaScript happen?
For zones with a time less than GMT, creating a date using the Date()
constructor with a date string in ISO Dates format is a valid date, but the value of the getDate()
returns another date smaller than the one we created.
Example:
const date = new Date("2020-03-10"); console.log("The date is: " + date.getDate());
Output:
The date is: 9
We see these returns differently than expected. After I checked, I got the following date string:
'Mon Mar 09 2020 21:00:00 GMT-0300 (Brazil Standard Time)'
So the date we get through this date()
constructor will be the 9th, not the 10th because my browser’s zone time is GMT -0300, so the generated date will be subtracted 3 hours. Since then, the date we create will be changed. In order not to say unexpected results on I will show you the following ways:
- Use short dates or long dates formats to create new dates
- Pass in the constructor the hour minute, the second value of the newly created date
With these two ways, I tried and tested and found that getDate() returns the wrong date in JavaScript and no longer appears.
How to fix this error?
Use short dates or long dates formats to create a new date
The ISO 8601 syntax (YYYY-MM-DD) is a popular choice when we want to create a new date, but with ISO 8601, we often encounter the getDate()
method that returns the wrong date for hostile GMT areas. So I advise using short dates or long dates formats to create new dates.
Because when creating a new date with ISO dates format, the result returned is a date that depends on the time of the browser you are using.
Example:
// For the GMT -0300 const date1 = new Date("2020-03-10"); // Mon Mar 09 2020 21:00:00 GMT-0300 // For the GMT +0100 const date2 = new Date("2020-03-10"); // Tue Mar 10 2020 01:00:00 GMT+0100
The example clearly shows why the getDate()
method returns the wrong date. But with long dates and short dates format, we are different when we use these two date formats to create a new date. The new date will create a new date with a time of ’00:00:00′. Then there will be no status. getDate()
returns an unexpected date anymore.
Short Dates
Syntax:
MM/DD/YYYY
Example:
// For the GMT -0300 const date1 = new Date("03/10/2020"); // Tue Mar 10 2020 00:00:00 GMT-0300 // For the GMT +0100 const date2 = new Date("03/10/2020"); // Tue Mar 10 2020 00:00:00 GMT+0100
Long Dates
Syntax:
MMM DD YYYY
Example:
//For the GMT -0300 const date1 = new Date("Mar 25 2015"); // Tue Mar 10 2020 00:00:00 GMT-0300 //For the GMT +0100 const date2 = new Date("Mar 25 2015"); // Tue Mar 10 2020 00:00:00 GMT+0100
With the created dates created from the input parameter with the format short dates or long dates, we will always get the expected results using getDate()
.
Pass in the constructor the hour, the minute, and the second value of the newly created date
We can use a date string with ISO dates format to pass as a parameter to the date()
constructor without problems as long as the getDate()
method returns the wrong date. This string is ISO Dates (Date-Time) with the following information. Information about the time of the date we create, then the program will know the exact date we want to create with the time you have entered in full.
Syntax:
YYYY-MM-DDTHH:MM:SS
Example:
//For the GMT -0300 const date1 = new Date("2020-03-10T00:00:00"); // Tue Mar 10 2020 00:00:00 GMT-0300 //For the GMT +0100 const date2 = new Date("2020-03-10T00:00:00"); // Tue Mar 10 2020 00:00:00 GMT+0100
Similar to using short dates or long dates format, we will get the expected return results; otherwise, it won’t happen that the getDate()
method returns the wrong date, no matter what your GMT zone is.
Summary
Above are the two ways I used to solve the problem getDate() returns the wrong date in JavaScript. To get a correct result, I recommend that you pass in the constructor the hour, the minute, and the second value of the newly created date. It will help make the date you create more accurate. Please try to follow me. This will help you better understand and remember it longer. This article will help you and your program. Good luck!
Maybe you are interested:
- Date.getDay() returns wrong day in JavaScript
- Change getDate() to 2 digit format in JavaScript
- Check if a Date is less than 1 Hour ago using JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css