76

I am using this bootstrap-datepicker for my datepicker. I'd like the datepicker to choose "today" for start day or default day. I cannot figure out how to set "today" automatically, so I did an inefficient way

HTML:

<input type="text" value ="today();" id="datepicker"/>

JS:

 $('#datepicker').datepicker();
function today(){
    var d = new Date();
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    document.write(curr_date + "-" + curr_month + "-" + curr_year);
}

Online Sample: http://jsfiddle.net/BXZk2/

Just looking for an easy solution to make the default day as "TODAY".

Thanks

tech_geek
  • 1,624
  • 3
  • 21
  • 44
olo
  • 5,225
  • 15
  • 52
  • 92

13 Answers13

95

This should work:

$("#datepicker").datepicker("setDate", new Date());

And for autoclose (fiddle):

$('#datepicker').datepicker({
        "setDate": new Date(),
        "autoclose": true
});

jQuery > datepicker API

Nic
  • 13,287
  • 7
  • 40
  • 42
  • So this way is not working? `$('#datepicker').datepicker({ autoclose: true, setDate: new Date() });` – olo Mar 15 '13 at 02:09
  • 1
    Doesn't look like `autoclose` is a valid option, that may be why. – Nic Mar 15 '13 at 02:16
  • but it works this way `$('#datepicker').datepicker({ autoclose: true, }); ` – olo Mar 15 '13 at 02:17
  • 11
    I currently have two blocks `$("#datepicker").datepicker("setDate", new Date());` and `$('#datepicker').datepicker({ autoclose: true, });` this way works – olo Mar 15 '13 at 02:18
  • Check the update, that worked for me (autoclose is a bootstrap-specific function apparently) – Nic Mar 15 '13 at 02:21
  • 1
    Thanks for the fiddle, that one doesnt work as the default date is empty. this one works the same as what I am having now. Separate blocks http://jsfiddle.net/aekjD/ – olo Mar 15 '13 at 02:27
  • 27
    This answer is invalid actually, because it talk about `jquery datepicker` and the question is about `bootstrap datepicker` – Kishor Pawar Jun 21 '16 at 10:50
48

Set after Init

 $('#dp-ex-3').datepicker({ autoclose: true, language: 'es' });
$('#dp-ex-3').datepicker('update', new Date());

This example is working.

Marc Rubiño
  • 725
  • 7
  • 8
34

I used this code

$('#datePicker').datepicker({
                    format:'mm/dd/yyyy',
                }).datepicker("setDate",'now');
Shiva Manhar
  • 673
  • 8
  • 19
  • Thanks, this is the only way that works for me. jQuery UI - v1.12.1 - 2021-06-06, jQuery dependency v3.3.1 and I can NOT get those above work – Huy Phạm Oct 06 '21 at 09:56
16

Date picker with current date and basic settings:

// Datepicker
$('.datepicker').datepicker({
    autoclose: true,
    format: "yyyy-mm-dd",
    immediateUpdates: true,
    todayBtn: true,
    todayHighlight: true
}).datepicker("setDate", "0");
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
Ronak Bokaria
  • 616
  • 6
  • 6
12

If none of the above option work, use the following :

$(".datepicker").datepicker();
$(".datepicker").datepicker("setDate", new Date());

This worked for me.

JON
  • 965
  • 2
  • 10
  • 28
7

I used this a little example and it worked.

$('#date').datetimepicker({
    defaultDate: new Date()
});

demo : http://jsfiddle.net/m2fjw57b/1/

Bruno Ribeiro
  • 1,280
  • 16
  • 21
7

It works fine for me...

$(document).ready(function() {
    var date = new Date();
    var today = new Date(date.getFullYear(), date.getMonth(), date.getDate());

    $('#datepicker1').datepicker({
        format: 'dd-mm-yyyy',
        orientation: 'bottom'
    });

    $('#datepicker1').datepicker('setDate', today);

});
JON
  • 965
  • 2
  • 10
  • 28
AngularJMK
  • 1,178
  • 13
  • 15
6

For bootstrap date picker

$( ".classNmae" ).datepicker( "setDate", new Date());

* new Date is jquery default function in which you can pass custom date & if it not set, it will take current date by default

amn
  • 180
  • 2
  • 6
5

This question is about bootstrap date picker. But all of the above answers are for the jquery date picker. For Bootstrap datepicker, you just need to provide the defaut date in the value of the input element. Like this.

<input class="form-control datepicker" value=" <?= date("Y-m-d") ?>">
Yasir Ijaz
  • 674
  • 1
  • 12
  • 19
3

It's simple

$(function() {
    $("#datePicker").datetimepicker({
        defaultDate:'now'       
    });
});

This will be set today as the default

JON
  • 965
  • 2
  • 10
  • 28
Ritesh
  • 350
  • 3
  • 4
3

Simply put the following one. This works for me.

$('.className').datepicker('setDate', 'now');
Muhammad Saimon
  • 233
  • 2
  • 10
2

According to this link Set default date of jquery datepicker, the other solution is

var d = new Date();

var currDate = d.getDate();
var currMonth = d.getMonth();
var currYear = d.getFullYear();

var dateStr = currDate + "-" + currMonth + "-" + currYear;

$("#datepicker").datepicker(({dateFormat: "dd-mm-yy" autoclose: true, defaultDate: dateStr });
Community
  • 1
  • 1
Hensembryan
  • 1,067
  • 3
  • 14
  • 31
  • 4
    this question is about the bootstrap datepicker, which is similar, but uses a different set of options than the jquery UI datepicker. see https://bootstrap-datepicker.readthedocs.org/en/latest/options.html – calebtr Sep 03 '15 at 21:55
  • var currMonth = d.getMonth()+1; – Kurkula Apr 01 '16 at 04:30
-3

I changed code in line 1796 file 'bootstrap-datetimepicker.js'. It's worked for me

enter image description here

Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424