6

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'
});
user2672112
  • 199
  • 1
  • 14

6 Answers6

2

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
    });
});
fluminis
  • 3,575
  • 4
  • 34
  • 47
madhu
  • 244
  • 5
  • 13
  • 1
    Hi madhu, basically `minDate: 0` already disables the past date. But it doesn't disable the past time. For example, if the current time is 5 am, i like to disable even 4 am of the same day. :) – user2672112 Aug 10 '15 at 04:47
  • Yes, it disabled date correctly. How to disable the past time? – scalauser Aug 11 '15 at 11:52
2

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

$('.datepicker').pickadate({
        min: new Date()
    });
Rupal
  • 1,111
  • 6
  • 16
0

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'
});
spenibus
  • 4,339
  • 11
  • 26
  • 35
0

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

it should be like this:

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


                });
            });
Abdul Majeed
  • 2,671
  • 22
  • 26
0

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

thecodedeveloper.com
  • 3,220
  • 5
  • 36
  • 67