11

I'm trying to get bootstrap datepicker to highlight the date selected on the dropdown date picker. It's not currently doing this.. What am I missing?

<div class="input-append date" id="datepicker" data-date="dateValue: Customer.DateOfBirth" data-date-format="dd-mm-yyyy">
    <input class="span2" size="16" type="text" data-bind="value: Customer.DateOfBirth" readonly="readonly" />
    <span class="add-on"><i class="icon-calendar"></i></span>
</div>
Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
web-boy
  • 111
  • 1
  • 1
  • 4
  • I didn't have the datepicker.css file referenced in the code. Is there a way to get the highlighted date to be the data-bind date instead of todays date? – web-boy Nov 14 '12 at 14:12

2 Answers2

14

You have to call the datepicker via javascript $('#datepicker').datepicker();​.

To hightlight the date selected, just include datepicker.css

Code Example / Result

  • Updated bootstrap.css
agriboz
  • 4,724
  • 4
  • 35
  • 49
  • Thanks for the reply. I've already got the above code. I'm starting to think it's maybe the version of bootstrap I'm using as I think your using the latest version in the fiddle example. – web-boy Nov 14 '12 at 13:34
  • I didn't have the datepicker.css file referenced in the code. Is there a way to get the highlighted date to be the data-bind date instead of todays date? – web-boy Nov 14 '12 at 14:12
  • You can try `data-date`. For example `data-date="12-12-2012"` it will highlight 12-12-2012 – agriboz Nov 14 '12 at 14:29
  • Just curious.. How do you include twitter boostrap.js in fiddler? When I look at your fiddler example i'm only seeing jquery included and not the boostrap.js. – RayLoveless Oct 09 '13 at 23:02
  • I didn't include `bootstrap.js`. I think there is no need to include it in this case. – agriboz Oct 10 '13 at 09:32
  • @agribox, The bootstrap-datepicker.js code is what is allowing the date picker to appear on the screen. I can see it included when I use chrome dev tools. hmm...I wonder how it got there? – RayLoveless Oct 10 '13 at 15:11
  • does it work in bootstrap 3? I did it, but not working – Willy Apr 15 '14 at 11:19
  • @Willy there is an answer about using with bootstrap 3. This thread is about bootstrap 2. http://stackoverflow.com/questions/18887950/how-do-i-get-bootstrap-datepicker-to-work-with-bootstrap-3 – agriboz Apr 15 '14 at 11:49
-1
<linkrel="stylesheet"href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<script src="js/bootstrap.min.js" type="text/javascript"></script>

<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="http://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="js/bootstrap-datepicker.js" type="text/javascript"></script>
<script src="js/bootstrap-datepicker.min.js" type="text/javascript"></script>


<script>

    $(document).ready(function() {
        $('#datePicker')
            .datepicker({
                format: 'mm/dd/yyyy'
            })
            .on('changeDate', function(e) {
                // Revalidate the date field
                $('#eventForm').formValidation('revalidateField', 'date');
            });

        $('#eventForm').formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                name: {
                    validators: {
                        notEmpty: {
                            message: 'The name is required'
                        }
                    }
                },
                date: {
                    validators: {
                        notEmpty: {
                            message: 'The date is required'
                        },
                        date: {
                            format: 'MM/DD/YYYY',
                            message: 'The date is not a valid'
                        }
                    }
                }
            }
        });
    });

</script>

<div class="form-group">
    <div class="date">
        <div class="input-group input-append date" id="datePicker">
            <input type="text" class="form-control" name="date" />
            <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
        </div>
    </div>
</div>

for this u have to had bootstrap-datepicker.js & bootstrap-datepicker.min.js files.U can also call $('#datePicker').datepicker(); method but it comes with its own default settings. So in the script tag I have applied my own customization in the datepicker() method. Then I just make an datepicker input component in the div tag which is defined in the body tag but I have meke it briefly.

The solution of u r question is the span tags. Which r applying icons on the datepicker input component. I have choosen an calender icon which is glyphicon glyphicon-calendar. But before it have to tell the browser that I am going embedd an icon by writing class="input-group-addon add-on" in the span tag which is used to group icon with the text input.

Harpreet Singh
  • 329
  • 3
  • 8