Warning: session_start(): open(/tmp/sess_2610e40660bc4c0bd3bc586fbb946634, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How to solve Date.getDay() returns wrong day in JavaScript - LearnShareIT

How to solve Date.getDay() returns wrong day in JavaScript

Date.getDay() returns wrong day in JavaScript

“Date.getDay() returns wrong day” in JavaScript is a common problem related to a date getDay() method. The below explanations can help you know more about the causes and solutions of this error.

Why does the error “Date.getDay() returns wrong day” in JavaScript happen?

In fact, getDay() is a method that belongs to new Date objects. Its functionality is returning an integer number from 0 to 6 corresponding to the day (according to local time) of the week. The method may sometimes be misled by programmers to get the date in a date object. 

For example, if the date is Sunday, the result returned will be zero:

// Create a date object
let myDate = new Date();
console.log(myDate);

// Using getDay returns the wrong day instead of 11
console.log(myDate.getDay());

Output:

Sun Dec 11 2022 09:39:41 GMT+0700
0

Here is another example:

// Create a date object
let myDate = new Date("Dec 31, 2022");
console.log(myDate);

// Using getDay returns the wrong day instead of 31
console.log(myDate.getDay());

Output:

Sat Dec 31 2022 00:00:00 GMT+0700
6

As you can see, neither the getDay() method returns 11 in the first example nor 31 in the second example, as they are your expected values. In the first example, the method return 0 indicates that the day is Sunday. In the second example, 6 is returned as it is represented for Saturday of 31/12/2022.

To get the correct date, such as 11 in the first example or 31 in the second one, we suggest you check out the following solutions.

How to solve the error “Date.getDay() returns wrong day” in JavaScript?

Using getDate() instead

You should remember that the date of a date object is different from the weekday offset of the date object. In fact, the getDay() returns the day of the week (starts from 0 on Sunday and ends at 6 on Saturday). Therefore, if you want to get the date (the day number in the calendar), you must use the getDate() method instead. For example:

// Create a date object
let myDate = new Date();
console.log(myDate);

// Using getDate() returns the correct day
console.log(myDate.getDate());

Output:

Sun Dec 11 2022 10:02:00 GMT+0700
11

Here is another example:

// Create a date object
let myDate = new Date("Dec 31, 2022");
console.log(myDate);

// Using getDate() returns the correct day
console.log(myDate.getDate());

Output:

Sat Dec 31 2022 00:00:00 GMT+0700
31

Have you found the expected results produced by this getDate() method? If that is not what you want, but you want 2 for Monday and 7 for Saturday or some number you what, please read the next solution.

Adding 1 to the getDay()

If you already have knowledge of what getDay() returns and you think this is not because of your misunderstanding. Then you may want to change the day of the week according to its name instead of its offset. For instance, 1 represents Sunday:

// Create a date object
let myDate = new Date();
console.log(myDate);

// Using getDay()+1 returns the correct day
console.log(myDate.getDay() + 1);

Output:

Sun Dec 11 2022 10:02:00 GMT+0700
1

Here is another example of 7 for Saturday:

// Create a date object
let myDate = new Date("Dec 31, 2022");
console.log(myDate);

// Using getDay()+1 returns the correct day
console.log(myDate.getDay() + 1);

Output:

Sat Dec 31 2022 00:00:00 GMT+0700
7

The logic behind this method is very simple, as the getDay() method returns the day of the week (starts from 0 on Sunday), so we just need to add 1 to the result, and we will get the correct day of the week (2 for Monday and 1 for Sunday…).

Summary

This article explains why Date.getDay() returns the wrong day in JavaScript. In fact, what solution to this problem depends on your desired results as you are wanting the normal day of the week or the date instead. We hope that this tutorial will help you get what you want.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *