3

There are two Jquery datepickers in use, StartDate and EndDate

<input id="StartDate" class="datepicker setNext hasDatepicker" type="text" value="13/02/2015" name="StartDate">
<input id="EndDate" class="datepicker hasDatepicker" type="text" value="15/02/2015" name="EndDate">

When the StartDate datepicker is selected I want the EndDate datepickers to be the StartDate + 1 Day, and to make it so that earlier dates cannot be selected in EndDate than are in StartDate.

I have this code:

$(function () {
    $(".datepicker").datepicker({
        dateFormat: 'dd/mm/yy',
        onSelect: function( selectedDate ) {
            if(this.id == 'StartDate') {
                var minDate = selectedDate + 1;
                $('#to').datepicker("option", "minDate", minDate);

            }
        }
   });
});

So it hits the onSelect ok, but then the adding 1 to the date doesn't work (I just get the datestring with a 1 on the end, so '31/12/20141').

I have also tried the following in the OnSelect, assuming that selectedDate was a string not a date type:

                var minDate = new Date(selectedDate);
                var tomorrow = new Date();
                tomorrow.setDate(minDate.getDate() + 1);
                $('#to').datepicker("option", "minDate", tomorrow);

minDate ends up being an Invalid Date, as does tomorrow. I can't work out how to set dates from the string. Trying something along the lines of:

var minDate = selectedDate.getDate();

Gets me an 'Uncaught TypeError: undefined is not a function'.

Using JQuery 1.11.1, UK date formats of dd/mm/yyyy.

2
  • What happens if you try something like this: var date2 = $('#StartDate').datepicker('getDate', '+1d'); do you'll get a valid date for date2? Commented Dec 31, 2014 at 12:46
  • 1
    The parsing of a string to a date does not use your display formatting, so dd/mm/yy returns invalid dates in new Date(selectedDate) Commented Dec 31, 2014 at 12:49

4 Answers 4

2

If you want to change the datepicker date of another datepicker adding a day, you can do something like this:

Note that selectedDate is in format dd/mm/YYYY which is not a valid string date format to use in Date constructor. You have then to parse it.

        var arr = selectedDate.split("/");
        var date = new Date(arr[2]+"-"+arr[1]+"-"+arr[0]);
        var d = date.getDate();
        var m = date.getMonth();
        var y = date.getFullYear();
        var minDate = new Date(y, m, d + 1);
        $("#EndDate").datepicker('setDate', minDate);

Here you have a working example fiddle

6
  • I take that back... The Javascript library is insane :) Commented Dec 31, 2014 at 12:59
  • Added my own version, using getDate() + 1 which looks less insane (but yes, yours also works) :) Commented Dec 31, 2014 at 13:03
  • Deleted my own answer as the whole setDate(getDate() + 1) appears broken. Commented Dec 31, 2014 at 13:20
  • @TrueBlueAussie The problem you had was as follows. If you don't create the Date object with the year and the month selected, it'll assume you want to use current month and year resulting in 12 and 2014 that I saw. This is the reason why the "oddity" you mentioned in my post is not an oddity at all. You create the object with the month and year desired and add a day to the day. The object is clever enough to figure out that 29 means current month+1 and resets day to 1 too. Well, I think it works that way.
    – acontell
    Commented Dec 31, 2014 at 13:23
  • Perfect, thanks! Whilst I was aware that I had to format the date, I forgot that the parser wouldn't like the new format!
    – UglyTeapot
    Commented Dec 31, 2014 at 13:27
1

here i am letting the user select interval of 10 days only..you can modify it according to your requirement.

$(document).ready(function() {
    var minDate ;
    var maxDate;
    var mDate
    var j = jQuery.noConflict();
    j( "#startTime" ).datepicker({
    onSelect: function() {
    minDate = j( "#startTime" ).datepicker("getDate");
    var mDate = new Date(minDate.setDate(minDate.getDate()));
    maxDate = new Date(minDate.setDate(minDate.getDate() + 10));
j("#endTime").datepicker("setDate", maxDate);
j( "#endTime" ).datepicker( "option", "minDate", mDate);
j( "#endTime" ).datepicker( "option", "maxDate", maxDate);
}
});
var tdate = new Date();
var ddate = new Date(tdate.setDate(tdate.getDate() + 10));
j("#startTime").datepicker("setDate", new Date());
j( "#endTime" ).datepicker();
j("#endTime").datepicker("setDate",ddate);
  });
 <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script><div>Please Select Dates:</div>
	 <p>Strat Date:<input id ="startTime" type="text" ></input></p>
	 <p>End Date:<input id ="endTime" type="text" ></input></p>

0

You can use the setDate and getDate.

Example:

<input id="StartDate" class="datepicker setNext hasDatepicker" type="text" value="13/02/2015" name="StartDate">
<script>
function convertInputtoDate(){
    var input = document.getElementById("StartDate").value;
    var date = input.split('/');
    var selectdate = new Date(date[2], date[1], date[0]);

    return selectdate;
}
var today = convertInputtoDate();
var tomorrow = new Date(convertInputtoDate());
tomorrow.setDate(today.getDate() + 1);
alert('Today : ' + today + ' Tommorow :' + tomorrow.toLocaleString());
</script>

If you want to change the date format, use:

tomorrow.toLocaleString(); // output will be : d/m/Y H:M:S

And to convert your input to date:

function convertInputtoDate(){
    var input = document.getElementById("StartDate").value;
    var date = input.split('/');
    var selectdate = new Date(date[2], date[1], date[0]);

    return selectdate;
}
4
  • 1
    True, but you need to apply this to the specific question as the dd/mm/yy date format is also a problem. Commented Dec 31, 2014 at 13:04
  • Can use tomorrow.toLocaleString()
    – Invicnaper
    Commented Dec 31, 2014 at 13:11
  • That still does not match the question... You need to take the start date, not today. Try completing a JSFiddle. Commented Dec 31, 2014 at 13:12
  • You need to ensure your answer matches the question asked. Suggest you create a JSFiddle matching the example shown. Commented Dec 31, 2014 at 13:29
0

You can even use something like this.

 $(function() {
  $('#date-range').daterangepicker({
    startDate: moment(),
    endDate: moment(),
    timePicker: true,
    timePicker24Hour: true,
    timePickerIncrement: 15,
    locale: {
      format: 'M/DD/YYYY h:mm A'
    }
  });
});
<!-- jQuery -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

<!-- Moment.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

<!-- Date Range Picker -->
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<label for="date-range">Date Range:</label>
<input type="text" id="date-range" name="date-range" />

   

Not the answer you're looking for? Browse other questions tagged or ask your own question.