-1

I have 2 datepickers. #datepicker0 and #datepicker1 and the following code to check if the date_from is after date_to:

$('#datepicker0').datepicker({
  dateFormat: 'yy-mm-dd'
});

var valeDate = {
  dateFormat: 'yy-mm-dd',
  onSelect: function() {
    if ($('#datepicker0').val() > $(this).val()) {
      alert("Date problem");
      $(this).val(null)

    }

    $(this).change();
  }
}

$("#datepicker1").datepicker(valeDate).on("change", function() {
  display("Change event");

I would like to remove the parameter #datepicker0 from the onSelect function in order to make the function reusable.

Could anyone show me how to make it?

rugby82
  • 679
  • 1
  • 9
  • 25
  • https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – ellipsis Aug 07 '19 at 10:14
  • Convert the dates into timestamps and check wether the number is greater than the other. – mind Aug 07 '19 at 10:18
  • If the value of date2 cannot be before date1, update the `minDate` of `date2` when `date1` has a value set. Then you don't need to validate anything client-side. See [this question](https://stackoverflow.com/questions/5375373/jquery-datetime-picker-set-mindate-dynamic) for more information – Rory McCrossan Aug 07 '19 at 10:25
  • no sorry...the function posted is working fine....i would remove from it the dependency by the element "datepicker0". – rugby82 Aug 07 '19 at 10:26
  • More specifically i would like to improve my code from "if ($('#datepicker0').val() > $(this).val()) " to " if ($(generic_value).val() > $(this).val()) " – rugby82 Aug 07 '19 at 10:32

2 Answers2

1
function validateDate(datepicker, value) {
   if (value > datepicker.val()) {
       alert("Date problem")
       datepicker.val(null)
   }
   datepicker.change()
}

var valeDate = {
  dateFormat: 'yy-mm-dd',
  onSelect: function() {
      validateDate($(this), $('#datepicker0').val())
  }
}

Next time, please address questions like this (everything works but code require modifications) to code review (https://codereview.stackexchange.com/)

ymz
  • 6,602
  • 1
  • 20
  • 39
0

finally i used this code:

function datepickerValidator(startDate,endDate) {
   var datepickerFrom = startDate;
   var datepickerTo   = endDate;

// returns the millisecond obtained from the string 'dd-mm-yy'
   function getTimeMillis(dateString) {
      var splittedFrom = dateString.split("-");
      var day = splittedFrom[0];
      var month = splittedFrom[1];
      var year = splittedFrom[2];
      var dateTmp = new Date(year + "-" + month + "-" + day);
      var timeMillis = dateTmp.getTime();
    return timeMillis;
}

var validateDatePickerOption = {dateFormat: 'dd-mm-yy',
        onSelect: function() {
            from = getTimeMillis(datepickerFrom.val());
            to = getTimeMillis($(datepickerTo).val());
            if ( from > to )  {
                alert("Date problem: value 'To' must be after 'From'");
                $(this).val(null)
                }
            }
        }
 return validateDatePickerOption;
}

and for each datepicker:

var from = $("#From");
var to = $("#To");
var datepickerOption = datepickerValidator(from,to);
from.datepicker(datepickerOption);
to.datepicker(datepickerOption);
rugby82
  • 679
  • 1
  • 9
  • 25