0

I have a two textboxes which I'll get the date to compare in the format of yy-mm-dd.

// Get the value from textbox
var date1 = $("#f_date").val();    // 2015-01-01
var date2 = $("#t_date").val();    // 2015-03-01

if(date1 < date2) {
    alert("From date is lesser than To Date");
}
else {
    alert("From date is greater than To Date");
}

How can I do that? Please help. Thanks

Larcy
  • 289
  • 3
  • 8
  • 18

2 Answers2

6

You can do just like that.

As the date format has the components in falling magnitude, and the lengths of the components are the same, you can just compare them as strings.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

As Mentioned by lolbas, convert date1 and date2 to JS Date() object and compare.

Here's the Code Snippet for your reference:

  function CompareDates() {

            var date1 = new Date(document.getElementById('f_date').value);
            var date2 = new Date(document.getElementById('t_date').value);
            if (date1 > date2) {
                alert(date1 + " is later than " + date2 + ".");
            } else {
                alert(date2 + " is later than " + date1 + ".");

            }
        }
Sunil
  • 11
  • 3