I was browsing through the net to find a javascript function which can check whether the date entered by the user is current date or the future date but i didn't found a suitable answer so i made it myself.Wondering If this can be achieved by one line code.
function isfutureDate(value) { var now = new Date; var target = new Date(value); if (target.getFullYear() > now.getFullYear()) { return true; } else if(target.getFullYear() == now.getFullYear()) { if (target.getMonth() > now.getMonth()) { return true; } else if(target.getMonth() == now.getMonth()) { if (target.getDate() >= now.getDate()) { return true; } else { return false } } } else{ return false; } }
Approach 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.
Java 8 onward date API provides isBefore(), isEqual(), isAfter() and these methods take an input date value which is then compared to another date value to check whether the input date is before, equal or after another date value. That's all about checking the given date is past, future or today's date.
You can compare two dates as if they were Integers:
var now = new Date(); if (before < now) { // selected date is in the past }
Just both of them must be Date.
First search in google leads to this: Check if date is in the past Javascript
However, if you love programming, here's a tip:
Enjoy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With