Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show alert if user selects a date within the next 3 days

Im using xdsoft datetimepicker so users can select a deadline. I would like to show an alert if they choose one of the next 3 days.

Initially I had set a minDate so these days were not selectable but I would prefer if these days can be selected and an alert box is shown instead.

I am not sure if there if an option for this already exists, I could not find one in the documentation.

This is how I've been doing it-

html

<h3>Deadline:</h3>
<input type="text" id="datetimepicker"/>

jquery

$('#datetimepicker').datetimepicker({
    timepicker:false,
    format:'d/m/Y',
    formatDate:'Y/m/d',
    minDate:'+1970/01/04',
});

I have also set up a jsfiddle

Thanks guys, if you could steer me in the right path it would be much appreciated

like image 524
Jalapeno Jack Avatar asked Jun 08 '17 13:06

Jalapeno Jack


1 Answers

check this fiddle for solution. I have used hard coded dates but you can do formatting accordingly. There is onSelectDate:function( ct ){..... that you can use.

$('#datetimepicker').datetimepicker({
    timepicker:false,
    format:'d/m/Y',
    formatDate:'Y/m/d',
    minDate:'+1970/01/00',
  onSelectDate:function( ct ){

  var dateFrom = "06/08/2017";
  var dateTo = "06/11/2017";
  var dateCheck = "06/09/2017";

  var d1 = dateFrom.split("/");
  var d2 = dateTo.split("/");
  var c = dateCheck.split("/");

  var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
  var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
  var check = new Date(c[2], parseInt(c[1])-1, c[0]);

    alert(check > from && check < to);
  },
});

https://jsfiddle.net/oqw90cbu/5/

like image 106
tech2017 Avatar answered Oct 27 '22 16:10

tech2017