-1

I have pretty extensively researched this issue, and I've found some useful information, but I haven't been able to solve my problem. All I'm trying to do is parse a date and compare it to another date. Seems simple, right? Here is what I've tried:

    function getCurrentDate() { //this function simply returns today's date
                var today = new Date();
                var dd = today.getDate();
                var mm = today.getMonth() + 1; 
                var yyyy = today.getFullYear();

                if (dd < 10) {
                    dd = '0' + dd
                }

                if (mm < 10) {
                    mm = '0' + mm
                }
                today = mm + '/' + dd + '/' + yyyy;
                return today; 
}

$("#TxtDate").blur(function () {
            var projectDueDate = Date.parse($("#lblDueDate").val()); //parses the project due date label to create a date variable
            var itemDueDate = new Date($("#TxtDate").val()); //parses the value the user entered into the due date box to create a date variable
            var actualProjectDueDate = new Date(projectDueDate);

            if (Date.parse(document.getElementById('TxtDate').value) > getCurrentDate()) {
                alert("The date you entered precedes today's date. Please enter a valid date.");
                $("#TxtDate").val() = "";
            }
        });

The if statement isn't working in the TxtDate blur function. It is not showing the alert window, even though I am entering a date that precedes today's date. As you can see, I've tried some different things. Any suggestions?

ic3man7019
  • 721
  • 6
  • 24
  • 3
    When dealing with dates everything *seems* simple. Its not. use a library - http://momentjs.com – Jamiec Jun 21 '16 at 14:07
  • 1
    You're comparing a date to a string... – Dave Newton Jun 21 '16 at 14:07
  • BTW, bith answers are correct, but for that matter why go from `Date` to `string` and back to `Date` - you already know how to get today's date as a date object (you did it in `getCurrentDate`) – Jamiec Jun 21 '16 at 14:10
  • Thanks to everyone for the helpful pointers. I knew it had to be something I was simply missing. – ic3man7019 Jun 21 '16 at 14:14

2 Answers2

0

Date.parse() returns a date object while getCurrentDate() returns a string. Add the Date.parse() there too:

if (Date.parse(document.getElementById('TxtDate').value) > Date.parse(getCurrentDate()))
Gerald Schneider
  • 17,416
  • 9
  • 60
  • 78
0

Your function getCurrentDate is returning a string not a date object and you are comparing it with date object. So you need to parse the return value of getCurrentDate.

if (Date.parse(document.getElementById('TxtDate').value) > Date.parse( getCurrentDate())) {
  alert("The date you entered precedes today's date. Please enter a valid date.");
  $("#TxtLeaveFrom").val() = "";
}
Mairaj Ahmad
  • 14,434
  • 2
  • 26
  • 40
  • Thanks for the help. You're right; however it appears that an additional problem has occurred. `Date.parse(document.getElementById('TxtDate').value` is returning an unintelligible value : `1465876800000`. Do you know how to get around this? – ic3man7019 Jun 21 '16 at 14:18
  • yes `Date.parse` returns something like this – Mairaj Ahmad Jun 21 '16 at 14:20