26

I want to restrict the date picker of bootstrap from taking future date.I just want to enable the dates up to today only.How can i achieve this.

Here is my code

<script>
  var FromEndDate = new Date();

   $(function(){
     $('.datepicker').datepicker({
       format: 'mm-dd-yyyy',
       endDate: FromEndDate, 
       autoclose: true
     });
   });
</script>

Can any body help regarding this

Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
user2142786
  • 1,484
  • 9
  • 41
  • 74

10 Answers10

56

Here is a demo of a working solution:

http://jsfiddle.net/tjnicolaides/cjp7y/

<script>
$(function(){
    $('.datepicker').datepicker({
        format: 'mm-dd-yyyy',
        endDate: '+0d',
        autoclose: true
    });
});
</script>

edit: the version of Bootstrap Datepicker that supports startDate and endDate is here: https://github.com/eternicode/bootstrap-datepicker

The original project, which is the current top search result for the plugin, does not have that feature at this time: http://www.eyecon.ro/bootstrap-datepicker/

TJ Nicolaides
  • 965
  • 6
  • 11
  • i tried it but it is not working in my code. date picker is working fine but it is not restricting the future dates – user2142786 Jan 07 '14 at 05:17
  • Are you sure all your external libraries are linked correctly? Do you see any errors in your console when you try to activate the datepicker? – TJ Nicolaides Jan 07 '14 at 05:18
  • Also: what version of the Datepicker library are you using? – TJ Nicolaides Jan 07 '14 at 05:19
  • Are you able to link to a public URL where you can demonstrate this not working? – TJ Nicolaides Jan 07 '14 at 05:23
  • I think you may be using a version of the plugin that doesn't support the endDate property. The project was forked and I recommend replacing your version with the latest version from here: https://github.com/eternicode/bootstrap-datepicker/blob/master/js/bootstrap-datepicker.js – TJ Nicolaides Jan 07 '14 at 05:33
  • yes TJ Nicolaides i found it and now it is working fine...thanks for your support... :) – user2142786 Jan 07 '14 at 05:38
  • i used this one http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.2.0/js/bootstrap-datepicker.js – user2142786 Jan 07 '14 at 05:39
  • This answer is incomplete because it is version specific.There's a `maxDate:0` propertie that can solve some cases for different versions of the plugin. gl – Paulo Bueno Jul 13 '19 at 14:40
  • I had to use `endDate: '-1d';` in order to sucessfully disable tomorrows date. Otherwise it would still allow me to select the 8th (if today's date was the 7th). – Dawson Irvine Mar 08 '22 at 03:04
28

Set endDate property to +0d

<script> 
    $(function() {
        $('.datepicker').datepicker({
            format: 'mm-dd-yyyy',
            endDate: '+0d',
            autoclose: true
        });
    });
</script>

FIDDLE

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • it is not working in my browser. i am using google chrome to check it. – user2142786 Jan 07 '14 at 05:29
  • @PranavRam Don't try to use GitHub as a CDN; it doesn't work. When viewing your fiddle in Chrome, it doesn't work and I see `Refused to execute script from 'https://raw.github.com/eternicode/bootstrap-datepicker/master/js/bootstrap-datepicker.js' because its MIME type ('text/plain') is not executable, and strict MIME type checking is enabled.` in the console. – Mark Amery Jan 17 '14 at 17:32
  • 1
    Thanks Pranav, it works like a charm. I tried lot of other options like maxDate: '0' etc. that failed to work. But your answer saves my day. – AKS Oct 28 '15 at 07:43
7

This code worked for me..

$('#txtTo').datepicker({
    dateFormat: "dd/MM/yy",
    changeMonth: true,
    changeYear: true,
    ignoreReadonly: true,
    maxDate: 'now'
});
AndrewRMillar
  • 608
  • 1
  • 7
  • 17
Nithya
  • 1,029
  • 2
  • 14
  • 27
6

Try this,

$(function () {
    $('.datepicker').datepicker({
        format: 'mm-dd-yyyy',
        endDate: '+0d',
        autoclose: true
    });
});

I used date picker from Git

Demo

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • Try it like, var today = new Date(); $('.datepicker').datepicker({ startDate: new Date(today.getFullYear()-5, today.getMonth(), today.getDate()) }); For more details see the issue https://github.com/uxsolutions/bootstrap-datepicker/issues/485 – Rohan Kumar Feb 16 '17 at 07:03
  • Now the Datepicker startDate option will takes Date parameter see https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#startdate – Rohan Kumar Feb 16 '17 at 07:04
6
$(document).ready(function(){
    $(".datepicker").datepicker({
        endDate:'today'
    });
});

Adding this to your js user can only select date upto today's date and user cannot enter custom date value manually to the input element. If user enters a custom value then it will automatically change to the current date

AndrewRMillar
  • 608
  • 1
  • 7
  • 17
KATJ Srinath
  • 950
  • 1
  • 11
  • 18
2

Due to issue in plugin it is getting happened use this datepicker.js

http://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.2.0/js/bootstrap-datepicker.js

user2142786
  • 1,484
  • 9
  • 41
  • 74
1
$(function(){
    $('.datepicker').datepicker({
        format: 'mm-dd-yyyy',
        onRender:function(date){
            return date.valueOf() > FromEndDate.valueOf()?'disabled':'';
        }
     });
});
AndrewRMillar
  • 608
  • 1
  • 7
  • 17
Rachel
  • 23
  • 1
  • 1
  • 7
1

Non of the above solutions work for me. After alot of searching and debugging finally I fix it. I am sharing my solution below, If someone like me facing the same problem can try the following solution.

var now = new Date();
$('[name=depdate]').datepicker({
    format: 'yyyy-mm-dd',
    onRender: function(date) {
        if(date.valueOf() > now.addDays(15).valueOf()) {
            return 'disabled';
        } else if(date.valueOf() < now.valueOf()) {
            return 'disabled';
        }
    }
})
.on('changeDate', function(){
    $(this).datepicker('hide');
}).data('datepicker');
AndrewRMillar
  • 608
  • 1
  • 7
  • 17
MasoodRehman
  • 715
  • 11
  • 20
1

You can do it using the option endDate. All the dates after the date specified in endDate option are disabled. If you want to disable dates after today use this in your input tag:

<input type="text" data-provide="datepicker" data-date-end-date="0d">

Source: https://bootstrap-datepicker.readthedocs.io/en/stable/options.html#id5

-1

Try this:

var date=new Date();
$('#datetimepicker').datetimepicker({
    format: 'MM/dd/yyyy',
    language: 'en',
    startDate : new Date('2015-01-01'),
    endDate :  date
});
Draken
  • 3,134
  • 13
  • 34
  • 54