69

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

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

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

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

谢谢

4

13 回答 13

88

这应该有效:

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

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

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

jQuery > 日期选择器 API

于 2013-03-15T02:03:05.613 回答
42

初始化后设置

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

这个例子是有效的。

于 2014-01-14T11:09:30.420 回答
30

我用了这段代码

$('#datePicker').datepicker({
                    format:'mm/dd/yyyy',
                }).datepicker("setDate",'now');
于 2017-11-20T09:45:05.277 回答
14

具有当前日期和基本设置的日期选择器:

// Datepicker
$('.datepicker').datepicker({
    autoclose: true,
    format: "yyyy-mm-dd",
    immediateUpdates: true,
    todayBtn: true,
    todayHighlight: true
}).datepicker("setDate", "0");
于 2017-12-27T15:25:49.050 回答
12

如果上述选项均不起作用,请使用以下命令:

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

这对我有用。

于 2016-06-24T10:08:54.870 回答
7

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

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

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

于 2015-08-31T03:31:28.417 回答
7

这对我来说可以...

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

});
于 2019-05-21T06:29:28.990 回答
6

对于引导日期选择器

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

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

于 2017-11-28T10:17:26.440 回答
3

这个问题是关于引导日期选择器的。但以上所有答案都适用于 jquery 日期选择器。对于 Bootstrap 日期选择器,您只需在输入元素的值中提供默认日期。像这样。

<input class="form-control datepicker" value=" <?= date("Y-m-d") ?>">
于 2019-05-08T07:40:07.753 回答
3

简单的说下一个。这对我有用。

$('.className').datepicker('setDate', 'now');
于 2021-01-20T15:25:06.390 回答
2

根据此链接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 });
于 2013-03-15T02:37:28.293 回答
2

这很简单

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

这将在今天设置为默认值

于 2015-12-01T09:53:50.277 回答
-3

我更改了第 1796 行文件“bootstrap-datetimepicker.js”中的代码。它对我有用

在此处输入图像描述

于 2017-11-06T09:32:11.843 回答