Comparing dates in jquery

learning picture learning · Feb 15, 2011 · Viewed 60.6k times · Source

I am having the following codes and it though i'm having 01-Jan-2009 for DateTo and 03-Jan-2009 for DateFrom it's reading the values as NAN. Am I missing anything? I`m referencing

 var DateToValue = $("#DateTo").val();
      var DateFromValue = $("#DateFrom").val();

      if (Date.parse(DateToValue) <= Date.parse(DateFromValue)) {
          $("#DateFrom").val(DateToValue)
      }
  <script src="@Url.Content("~/Scripts/jquery-1.4.2.min.js")"
 type="text/javascript"></script>

 <script  src="@Url.Content("~/Scripts/jquery.datePicker.js")"
type="text/javascript"></script>

         <script src="@Url.Content("~/Scripts/jquery.validate.js")"
 type="text/javascript"></script>

Answer

Johnny DropTables picture Johnny DropTables · Feb 15, 2011

How about this?

  DateTime DateToValue = $("#DateTo").val();
  DateTime DateFromValue = $("#DateFrom").val();

  if (Date.parse(DateToValue) <= Date.parse(DateFromValue)) {
      $("#DateFrom").val(DateToValue)
  }