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";
}
}
Popular Posts
- Show / Hide div based on dropdown selected using jQuery
- Autosuggestion Select Box using HTML5 Datalist, PHP and MySQL with Example
- Infinite Scrolling on PHP website using jQuery and Ajax with example
- How to Convert MySQL Data to JSON using PHP
- Custom Authentication Login And Registration Using Laravel 8
- Image Lazy loading Using Slick Slider
- Slick Slider Basic With Example
- Google Login or Sign In with Angular Application
- How to change date format in PHP?
- php in_array check for multiple values
- Adaptive Height In Slick Slider
- Slick Slider Center Mode With Example
- How to Scroll to an Element with Vue 3 and ?
- JavaScript Multiple File Upload Progress Bar Using Ajax With PHP
- Slick Slider Multiple Items With Example