4

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>

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

@imraanstack1 You're welcome, and don't worry about it, we've all been there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.