Warning: session_start(): open(/tmp/sess_72275fb40fd4e6915ae94cfe062f682e, 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 A Date Is During The Weekend Using Javascript - LearnShareIT

How To Check If A Date Is During The Weekend Using Javascript

Check if a Date is during the Weekend using JavaScript

After learning JavaScript’s built-in methods, we found several methods to check if a date is during the weekend using JavaScript. Please read the instructions below to know the methods we apply.

Check if a date is during the weekend using Javascript

Combine toString() and substring() methods

toString() method

This method will return a date object as a string.

Syntax:

Date.toString()

Parameter:

  • None

substr() method

This method takes part of a string without affecting the parent string.

Syntax:

string.substr(start, length)

Parameters:

  • start: Start position to get a string part.
  • end: The number of characters you want to get.

Please look at the example below to understand how we do it.

Example:

// Create a date object with a string
var myDate = new Date("2022-11-27");

// Create a date object with a string
var todayDate = new Date("2022-12-02");

// Check the weekend
function checkIsWeekend(myDate) {
	var convertDate = myDate.toString();
	console.log("convertDate: ", convertDate);
	var getDay = convertDate.substr(0, 3);

	if (getDay === "Sun" || getDay === "Sat") {
		return true;
	} else {
		return false;
	}
}

console.log("Is todayDate a weekend?", checkIsWeekend(todayDate));
console.log("Is myDate a weekend?", checkIsWeekend(myDate));

Output

convertDate:  Fri Dec 02 2022 07:00:00 GMT+0700
Is todayDate a weekend? false
convertDate:  Sun Nov 27 2022 07:00:00 GMT+0700
Is myDate a weekend? true

To check if a date is during the weekend using JavaScript, we first convert the Date object to a string using the toString() method, then we use the substr() method with 2 parameters, 0 and 3, to get the date of the string (the first 3 characters are used to identify the date).

We take the string obtained from the substr() method and then compare it with the string “Sat” (corresponding to Saturday is the weekend) or “Sun” (corresponding to Sunday is the weekend); If true, return true; If false, return false.

Use getDay() method

This method will return the number from 0 -> 6 corresponding to a day of the week. Sunday = 0, Monday = 1, and the numbers corresponding to the following days.

Syntax:

Date.getDay()

Parameter:

  • None

Example:

// Create a date object with a string
var myDate = new Date("2022-11-27");

// Create a date object with a string
var todayDate = new Date("2022-12-02");

// Check the weekend
function checkIsWeekend(myDate) {
	switch (myDate.getDay()) {
		// 0 is Sunday
		case 0:
			return true;

		// 0 is Saturday
		case 6:
			return true;
		default:
			return false;
	}
}

console.log("Is todayDate a weekend?", checkIsWeekend(todayDate));
console.log("Is myDate a weekend?", checkIsWeekend(myDate));

Output

Is todayDate a weekend? false
Is myDate a weekend? true

In this example, we could use an if else statement but we use a switch statement because we want to innovate a bit. We use the getDay() method to get the number corresponding to the day of the week. And use the switch case statement to see how the case. If it is 0 (corresponding to Sunday) or 6 (corresponding to Saturday), it returns true; otherwise, it returns false.

Summary

This article of ours used the toString method in combination with the substr() method, the date object’s getDay method, to check if a date is during the weekend using Javascript. If you want to see more tutorials like this, follow us, we have lots of detailed and easy-to-understand tutorials for you.

Maybe you are interested:

Leave a Reply

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