AnotherUser
AnotherUser

Reputation: 147

HTML Comparing 2 Dates with Javascript

I'm trying to compare 2 dates using Javascript. If "myDateL" is after "mydateR" display a message box when the button is clicked.

What is wrong with my code?

I know I've seen a similar thread to this but I couldn't understand it. I hope someone can help me with this please.

<input type="Button" value="TwoDates" onClick="twoDates()">

<script>

     function twoDates() {

        var firstdate = new date(document.getElementById("mydateL").value);
        var seconddate = new date(document.getElementById("mydateR").value);

     if(firstdate > seconddate) {

          alert('Please change your return date.');
            }
}
</script>

Upvotes: 4

Views: 201

Answers (1)

blm
blm

Reputation: 2466

It's new Date(...), not new date(...). Date is the global object that holds dates and times, date would be a function you've declared called date. If you look at the console when you run this, you should see something like:

ReferenceError: date is not defined

Upvotes: 4

Related Questions