禁用datepicker过去的日期&时间

时间:2015-08-10 04:27:44

标签: jquery datepicker jquery-ui-datepicker uidatepicker

我使用了datepicker,我喜欢人们无法选择当天之前的日期和时间。我使用" minDate:0"禁用过去的日期,但有没有办法禁用过去的时间。例如,如果当前时间是凌晨5点,我不希望人们在同一天的凌晨3点或4点选择。

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    startService(new Intent(this, BroadcastService.class));
    Log.i(TAG, "Started service");
}

private BroadcastReceiver br = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {            
        updateGUI(intent); // or whatever method used to update your GUI fields
    }
};

@Override  
public void onResume() {
    super.onResume();        
    registerReceiver(br, new IntentFilter(BroadcastService.COUNTDOWN_BR));
    Log.i(TAG, "Registered broacast receiver");
    }

@Override
public void onPause() {
    super.onPause();
    unregisterReceiver(br);
    Log.i(TAG, "Unregistered broacast receiver");
}

@Override
public void onStop() {
    try {
        unregisterReceiver(br);
    } catch (Exception e) {
        // Receiver was probably already stopped in onPause()
    }
    super.onStop();
}
@Override
public void onDestroy() {        
    stopService(new Intent(this, BroadcastService.class));
    Log.i(TAG, "Stopped service");
    super.onDestroy();
}

private void updateGUI(Intent intent) {
    if (intent.getExtras() != null) {
        long millisUntilFinished = intent.getLongExtra("countdown", 0);
        Log.i(TAG, "Countdown seconds remaining: " +  millisUntilFinished / 1000);            
    }
}

6 个答案:

答案 0 :(得分:2)

声明dateToday变量并使用Date()函数进行设置。 然后使用该变量分配minDate,这是[{1}}。

的参数
datepicker

答案 1 :(得分:2)

要停用过去的日期,您需要将min定义为今天的日期,如下所示: -

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

答案 2 :(得分:0)

您应该将minDate值声明为dateToday,就像这样

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

答案 3 :(得分:0)

要停用过去的时间,您应该使用minDateTime。 像这样更新你的代码,

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

答案 4 :(得分:0)

它应该是这样的:

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


                });
            });

答案 5 :(得分:0)

Source

我面临同样的问题,使用DateTimePicker jQuery插件禁用过去的日期时间。但我找到了解决方案 -

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