anchal saini anchal saini - 8 months ago 49
Javascript Question

Check if date is current date or future date with datetime as input

I'm trying to check if the date entered in

datetime
field is a current or a future date. I've tried:



function validations(){
var value=document.getElementById("showdate").value;
if (new Date() > new Date(value)) {
alert("Past date");
}
}

<Form method="post" onsubmit="validations()" autocomplete>
<input type="datetime-local" name="showdate" class="right" required="required" id="showdate">
<input type="submit">
</form>





But this code will work only with the
date
field.

Answer Source

Generally, in JS, to compare dates you should try:

function isFutureDate(value) {
    d_now = new Date();
    d_inp = new Date(value)
    return d_now.getTime() <= d_inp.getTime();
}

Run regular comparison operators on the getTime function of a Date object.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download