W.Sun W.Sun - 3 months ago 25
jQuery Question

jquery datepicker getdate return null

when I use

JQuery
datapicker
getDate
, it returns null.
I need two
datepickers
, the second one should be smaller than the first one.

the JS file is like this:

var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var today = d.getFullYear() + '-' +
(month<10 ? '0' : '') + month + '-' +
(day<10 ? '0' : '') + day;

var startDate;
$( function() {
$( "#start-date" ).datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:'2016-01-01',
maxDate:today,
});


startDate =$( "#start-date" ).datepicker("getDate");
if(startSDate==null){
startSDate = '2016-01-01'
}

$( "#end-date" ).datepicker({
dateFormat: 'yy-mm-dd',
inline: true,
minDate:startDate,
maxDate:today,

});
} );


However, this does not work. Here is jsFiddle.

Answer

Please use the following code.

    'use strict';
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var today = d.getFullYear() + '-' +
    (month<10 ? '0' : '') + month + '-' +
    (day<10 ? '0' : '') + day;
var fullSDate;
$( function() {
    $( "#start-date" ).datepicker({
      dateFormat: 'yy-mm-dd',
        inline: true,
        minDate:'2016-05-12',
        maxDate:today,
        });

    $( "#end-date" ).datepicker({
            dateFormat: 'yy-mm-dd',
            inline: true,
            minDate:fullSDate,
            maxDate:today       
    });

    $("#end-date").focusin(function(){
        fullSDate =$( "#start-date" ).datepicker("getDate");
        if(fullSDate==null){ 
             fullSDate = '2016-01-01'
        }
        $( "#end-date" ).datepicker( "option", "minDate", fullSDate);

    });     
  } );

Refer this Fiddle