6
votes

I'm using datepicker and i like people not be able to select date and time previous to the present day. I used "minDate: 0" to disable the past date but is there a way to disable past time. For example, if the current time is 5am, i do not want people to select 3 or 4 am of the same day.

This is my code:

var dt;
dt = 
$('#datetimepicker3').datetimepicker({
    inline:true,
    minDate: 0,
    format:'m/d/Y H:i',
    formatDate:'d/m/Y'
});
6
hello sir did you get the answer - Remya R

6 Answers

2
votes

Declare dateToday variable and use Date() function to set it. Then use that variable to assign to minDate which is parameter of datepicker.

var dateToday = new Date(); 
$(function() {
    $( "#datetimepicker3" ).datepicker({
        numberOfMonths: 3,
        showButtonPanel: true,
        minDate: dateToday
    });
});
2
votes

To disable past dates you need to define min to today date like this:-

$('.datepicker').pickadate({
        min: new Date()
    });
0
votes

You should declare the minDate value to dateToday just like this

var dt; 
dt = 
$('#datetimepicker3').datetimepicker({
    inline:true,
    minDate: dateToday,
    format:'m/d/Y H:i',
    formatDate:'d/m/Y'
});
0
votes

To disable the past time, you should use minDateTime. Update your code like this,

var dt = new Date();
$('#datetimepicker3').datetimepicker({
    inline:true,
    minDate: 0,
    minDateTime: dt,
    format:'m/d/Y H:i',
    formatDate:'d/m/Y'
});
0
votes

it should be like this:

$(function () {
                $('#datetimepicker3').datetimepicker({
                     minDate: moment()


                });
            });
0
votes

Source

i was facing same problem disable Past date time with DateTimePicker jQuery plugin. but i have found solution for it-

var checkPastTime = function(currentDateTime) {

var d = new Date();
var todayDate = d.getDate();

// 'this' is jquery object datetimepicker
if (currentDateTime.getDate() == todayDate) { // check today date
    this.setOptions({
        minTime: d.getHours() + ':00' //here pass current time hour
    });
} else
    this.setOptions({
        minTime: false
    });
};

$('#datetimepicker7').datetimepicker({
    format:'Y-m-d H:i',
    minDate : 0,
    onChangeDateTime:checkPastTime,
    onShow:checkPastTime
});

demo