jQuery 今天作为默认引导日期选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15423467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:00:57  来源:igfitidea点击:

bootstrap datepicker today as default

javascriptjqueryhtmldate

提问by olo

I am using this bootstrap-datepickerfor 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

我正在为我的日期选择器使用这个bootstrap-datepicker。我希望日期选择器选择“今天”作为开始日或默认日。我无法弄清楚如何自动设置“今天”,所以我做了一个低效的方式

HTML:

HTML:

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

JS:

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/

在线示例:http: //jsfiddle.net/BXZk2/

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

只是在寻找一个简单的解决方案来将默认日期设置为“今天”。

Thanks

谢谢

回答by Nic

This should work:

这应该有效:

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

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

And for autoclose (fiddle):

对于自动关闭(小提琴):

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

jQuery > datepicker API

jQuery > 日期选择器 API

回答by Marc Rubi?o

Set after Init

初始化后设置

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

This example is working.

这个例子是有效的。

回答by Shiva Manhar

I used this code

我用了这个代码

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

回答by Huzaifa Saifuddin

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

如果以上选项都不起作用,请使用以下方法:

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

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

This worked for me.

这对我有用。

回答by Ronak Bokaria

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

回答by Bruno Ribeiro

I used this a little example and it worked.

我用了这个小例子,它奏效了。

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

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

演示:http: //jsfiddle.net/m2fjw57b/1/

回答by amn

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

* new Date 是 jquery 默认函数,您可以在其中传递自定义日期,如果未设置,则默认使用当前日期

回答by Mani

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

    });

回答by Ritesh

Its simple

这很简单

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

        });
    });

This will set today as default

这将今天设置为默认值

回答by brian

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

根据此链接Set default date of jquery datepicker,另一种解决方案是

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