olo
olo

Reputation: 5271

bootstrap datepicker today as default

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

Upvotes: 77

Views: 395795

Answers (14)

Anik Saha
Anik Saha

Reputation: 1

$(".datepicker").datepicker({
            todayHighlight: true,
            autoclose: true,
            format: 'dd-mm-yyyy'
        });

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

I used this it will worked. by aniksaha12

Upvotes: 0

Ronak Bokaria
Ronak Bokaria

Reputation: 626

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

Upvotes: 17

Ritesh
Ritesh

Reputation: 360

It's simple

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

This will be set today as the default

Upvotes: 3

AngularJMK
AngularJMK

Reputation: 1268

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

});

Upvotes: 9

Huzaifa Saifuddin
Huzaifa Saifuddin

Reputation: 1068

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

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

This worked for me.

Upvotes: 12

Muhammad Saimon
Muhammad Saimon

Reputation: 281

Simply put the following one. This works for me.

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

Upvotes: 3

Yasir Ijaz
Yasir Ijaz

Reputation: 1043

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

Upvotes: 5

amn
amn

Reputation: 180

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

Upvotes: 6

Shiva Manhar
Shiva Manhar

Reputation: 703

I used this code

$('#datePicker').datepicker({
                    format:'mm/dd/yyyy',
                }).datepicker("setDate",'now');

Upvotes: 36

Tuấn Dương Hồng
Tuấn Dương Hồng

Reputation: 11

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

enter image description here

Upvotes: -3

Bruno Ribeiro
Bruno Ribeiro

Reputation: 1410

I used this a little example and it worked.

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

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

Upvotes: 7

Marc Rubi&#241;o
Marc Rubi&#241;o

Reputation: 735

Set after Init

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

This example is working.

Upvotes: 49

Hensembryan
Hensembryan

Reputation: 1087

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

Upvotes: 2

Nic
Nic

Reputation: 13733

This should work:

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

And for autoclose (fiddle):

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

jQuery > datepicker API

Upvotes: 98

Related Questions