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

13 Answers 13

95

This should work:

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

And for autoclose (fiddle):

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

jQuery > datepicker API

7
  • So this way is not working? $('#datepicker').datepicker({ autoclose: true, setDate: new Date() });
    – olo
    Mar 15, 2013 at 2:09
  • 1
    Doesn't look like autoclose is a valid option, that may be why.
    – Nic
    Mar 15, 2013 at 2:16
  • 11
    I currently have two blocks $("#datepicker").datepicker("setDate", new Date()); and $('#datepicker').datepicker({ autoclose: true, }); this way works
    – olo
    Mar 15, 2013 at 2:18
  • 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 jsfiddle.net/aekjD
    – olo
    Mar 15, 2013 at 2:27
  • 28
    This answer is invalid actually, because it talk about jquery datepicker and the question is about bootstrap datepicker Jun 21, 2016 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.

2
  • 4
    Thanks! This is the only solution that works for the Bootstrap DatePicker plugin. You first instantiate a datepicker, then set the date on it.
    – Alex
    Feb 2, 2017 at 16:40
  • Works like a charm. Thanks! Jan 26, 2021 at 12:19
35

I used this code

$('#datePicker').datepicker({
                    format:'mm/dd/yyyy',
                }).datepicker("setDate",'now');
2
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");
1
  • Set as 3 days before: $('.datepicker'){.....}).datepicker("setDate", "-3");
    – Stephen
    Oct 6, 2022 at 23:15
12

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

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

This worked for me.

8

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);

});
7

I used this a little example and it worked.

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

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

2
  • 3
    You're using another plugin in your fiddle: bootstrap-datetimepicker. The one in question here is bootstrap-datepicker
    – olefrank
    Nov 16, 2015 at 12:06
  • 1
    @olefrank is right but It works for bootstrap-datetimepicker and worked for me. Thanks! Mar 4, 2016 at 16:20
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

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") ?>">
3

Simply put the following one. This works for me.

$('.className').datepicker('setDate', 'now');
3

It's simple

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

This will be set today as the default

1
  • 1
    this's working for "datetimepicker" plugin not for "datepicker". Jun 4, 2018 at 7:00
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 });
2
-3

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

enter image description here

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.