Bootstrap datepicker disabling past dates without

2020-01-26 06:46发布

I wanted to disable all past date before current date, not with current date. I am trying by bootstrap datepicker library "bootstrap-datepicker" and using following code:

$('#date').datepicker({ 
    startDate: new Date() 
});

It works fine. But it is disabling date till today.

As example if today is 04-20-2013 and i disable past dates by setting startDate: new Date(). but I am able to select date from 04-21-2013.

UPDATED: i can solve it as following for UTC zone:

var d = new Date();
options["startDate"] = new Date(d.setDate(d.getDate() - 1));

or startDate: "+0d"

But these methods don't work when UTC is a day ahead. For my client in California that means at 5:00 pm my client can no longer select his local current date as a valid date. In order to fix this I am temporarily using startDate: "-1d", but of course before 5 that means yesterday is visible.

Has anyone come up with a better method for now as I do not want to tell users to put in a UTC date?

Thanks in advance.

19条回答
手持菜刀,她持情操
2楼-- · 2020-01-26 06:54

It depends on what format you put on the datepicker So first we gave it the format.

    var today = new Date();
    var dd = today.getDate();
    var mm = today.getMonth()+1; //January is 0!

    var yyyy = today.getFullYear();
    if(dd<10){
        dd='0'+dd;
    } 
    if(mm<10){
        mm='0'+mm;
    } 
    var today = yyyy+'-'+mm+'-'+dd; //Here you put the format you want

Then Pass the datepicker (depends on the version you using, could be startDate or minDate which is my case )

    //Datetimepicker
    $(function () {
        $('#datetimepicker1').datetimepicker({
            minDate: today, //pass today's date
            daysOfWeekDisabled: [0],
            locale: 'es',
            inline: true,
            format: 'YYYY-MM-DD HH:mm', //format of my datetime (to save on mysqlphpadmin)
            sideBySide: true
        });
    });
查看更多
唯我独甜
3楼-- · 2020-01-26 06:57

Disable all past date

<script type="text/javascript">
        $(function () {
            /*--FOR DATE----*/
            var date = new Date();
            var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());

            //Date1
            $('#ctl00_ContentPlaceHolder1_txtTranDate').datepicker({
              format: 'dd-mm-yyyy',
              todayHighlight:'TRUE',
              startDate: today,
              endDate:0,
              autoclose: true
            });

        });
</script>

Disable all future date

 var date = new Date();
 var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());

   //Date1
   $('#ctl00_ContentPlaceHolder1_txtTranDate').datepicker({
       format: 'dd-mm-yyyy',
       todayHighlight:'TRUE',
       minDate: today,
       autoclose: true
   });
查看更多
唯我独甜
4楼-- · 2020-01-26 07:00

var nowDate = new Date();
var today = new Date(nowDate.getFullYear(), nowDate.getMonth(), nowDate.getDate(), 0, 0, 0, 0);
	$('#date').datetimepicker({
		startDate: today
	});

查看更多
等我变得足够好
5楼-- · 2020-01-26 07:02

You can use the data attribute:

<div class="datepicker" data-date-start-date="+1d"></div>
查看更多
Emotional °昔
6楼-- · 2020-01-26 07:04

use

startDate: '-0d'

Like

$("#datepicker").datepicker({
    startDate: '-0d',
    changeMonth: true
});
查看更多
做自己的国王
7楼-- · 2020-01-26 07:04

You can find your solution in this link below: https://codepen.io/ahmetcadirci25/pen/NpMNzJ

Thats work for me.

My code:

        var date = new Date();
        date.setDate(date.getDate());

        $('#datetimepicker1').datetimepicker({
            isRTL: false,
            format: 'dd.mm.yyyy hh:ii',
            autoclose: true,
            language: 'tr',
            startDate: date
        });
查看更多
登录 后发表回答