Menu Close

How to check the input date is equal to today’s date or not using JavaScript?

check date

In this article, given a date object and the task is to determine the given date is the same as today’s date or not with the help of JavaScript. Here, we are using two type of methods to find a given date is same as today’s date or not.

Method 1: Get the input date from user (var inpDate) and the today’s date by new Date(). Now, use .setHours() method on both dates by passing parameters of all zeroes. All zeroes are passed to make all hour, min, sec and millisec to 0. Now compare today’s date with given date and display the result.

Example:

var down = document.getElementById('codeamend'); 
		
		function codeamend() { 
			var date = 
				document.getElementById('date').value; 
			
			var inpDate = new Date(date); 
			var currDate = new Date(); 
			
			if(inpDate.setHours(0, 0, 0, 0) == 
					currDate.setHours(0, 0, 0, 0)) 
			{ 
				down.innerHTML = 
					"The input date is today's date"; 
			} 
			else { 
				down.innerHTML = "The input date is" 
					+ " different from today's date"; 
			}		 
		} 

Method 2: Similarly get the input date from user (var inpDate) and the today’s date by using new Date(). Now, we will use .toDateString() method on both dates to convert them to readable strings. Now compare today’s date with given date and display the result.

Example:

	var down = document.getElementById('codeamend'); 
		
		function codemend() { 
			var date = 
				document.getElementById('date').value; 
			
			var inpDate = new Date(date); 
			var currDate = new Date(); 
			
			if(currDate.toDateString() == 
						inpDate.toDateString()) 
			{ 
				down.innerHTML = 
					"The input date is today's date"; 
			} 
			else { 
				down.innerHTML = "The input date is" 
						+ " different from today's date"; 
			} 
		} 
Posted in HTML, JavaScript, Web Technologies

You can also read...