Warning: session_start(): open(/tmp/sess_9569b0d57c5fb7c2c4d49d310f893057, 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 Check If Date Is Monday Using JavaScript - LearnShareIT

How To Check If Date Is Monday Using JavaScript

This article will share how to check if date is Monday using JavaScript. We will do this using the toDateString() and getDay() methods. Let’s go.

How to check if date is Monday using JavaScript

We will use the following two solutions to check if a date is Monday:

  • Using the toDateString() method
  • Using the getDay() method

Now, we will go to each solution with specific examples.

Using the toDateString() method

The toDateString() method is used to return the date part (without the time part) as a string (in English) of a date object.

Syntax:

Date.toDateString()

Example:

// Create a date object
const date = new Date("2023-01-10");
console.log(date.toDateString());

Output:

Tue Jan 10 2023

The value returned by the toDateString() method has the following components:

  • Abbreviation of the day of the week (e.g., Mon, Tue, etc.)
  • Abbreviation of the month (e.g., Jan, Jun, etc.)
  • A two-digit number representing the day of the month
  • A number with at least four digits representing the year (e.g., 2023, 0999, etc.)

To check if the input date object is Monday using the toDateString() method, we have to check if the output string of this method contains the ‘Mon’ substring using the String.includes() method.

The String.includes() method will return true if a substring exists in the specified string.

The following example describes checking if date is Monday using the toDateString() method.

Example:

// Check if date is Monday
function isMonday(date) {
    // Check if the output string of the toDateString() method contains the 'Mon' substring
    return date.toDateString().includes("Mon");
}

// Create a date object
const date = new Date("2023-01-09");
console.log(isMonday(date));

Output:

true

Using the getDay() method

In this solution, we use the getDay() method to get the day of the week as an integer (0 to 6, Sunday is 0, Monday is 1, etc.) of the input date object.

The getDay() method has the following syntax:

Date.getDay()

The following example describes checking if date is Monday using the getDay() method.

Example:

// Check if date is Monday
function isMonday(date) {
    // Check if the output of getDay() method is equal to 1 (Monday)
    return date.getDay() === 1;
}

// Create a date object
const date = new Date("2023-01-09");
console.log(isMonday(date));

Output:

true

Click here to learn more about the JavaScript Date methods.

Summary

We have shared two solutions using the toDateString() and getDate() methods to check if date is Monday using JavaScrip. You can use any solution for your program. Thank you for reading.

Leave a Reply

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