简体   繁体   English

设置默认日期 Bootstrap Datepicker

[英]Set Default Date Bootstrap Datepicker

How to set a default date in Bootstrap Datepicker?如何在 Bootstrap Datepicker 中设置默认日期?

I want to set a date : 24/12/2006我想设置一个日期:24/12/2006

When I open datepicker it should show me this date select on calender.当我打开日期选择器时,它应该在日历上显示这个日期选择。

Code:代码:

$(function()
{

    $('#dob').datepicker(
    {
        <!--viewMode: "years",-->
        onRender: function(date) 
        {
         //return date.valueOf() > now.valueOf() ? 'disabled' : '';
        }       
    }
    );
});

Here is a fiddle:这是一个小提琴:

http://jsfiddle.net/ymp5D/204/ http://jsfiddle.net/ymp5D/204/

You can do:你可以做:

$('#dob').datepicker('setDate', new Date(2006, 11, 24));
$('#dob').datepicker('update');
$('#dob').val('');

Fiddle Demo小提琴演示

Above accepted solution is staled, To help others who has the newest files, if you have Version 4 or newer, and you want to call a method, here is an example for bootstrap datetimepicker method :上面接受的解决方案已经过时,为了帮助拥有最新文件的其他人,如果您有版本 4 或更新版本,并且您想调用一个方法,这里是引导日期时间选择器方法的示例:

$('#eventDate').data("DateTimePicker").date(moment(date).format('YYYY-MM-DD'));

bootstrap.datetimepicker format Option sample : bootstrap.datetimepicker 格式选项示例:

 $('#eventDate').datetimepicker({ format: 'YYYY-MM-DD' });

for more see here (but without valid sample :) ): http://eonasdan.github.io/bootstrap-datetimepicker/Options/有关更多信息,请参见此处(但没有有效示例 :)): http : //eonasdan.github.io/bootstrap-datetimepicker/Options/

If you want to make the change global, look for the following in bootstrap-datepicker.js:如果您想让更改全局化,请在 bootstrap-datepicker.js 中查找以下内容:

function UTCToday(){
var today = new Date();
return UTCDate(today.getFullYear(), today.getMonth(), today.getDate());

and hard code in your date, for example:并在您的日期中进行硬编码,例如:

function UTCToday(){
var today = new Date();
return UTCDate(2006, 00, 01);

Which will open on 1 January 2006. Note that for some reason January is '00' rather than '01'.将于 2006 年 1 月 1 日开放。请注意,出于某种原因,一月是“00”而不是“01”。

This works for me after a long hours of search , This solution is for Bootstrap datetimepicker version 4. when you have to set the default date in input field.经过长时间的搜索后,这对我有用,此解决方案适用于 Bootstrap datetimepicker 版本 4。当您必须在输入字段中设置默认日期时。

HTML HTML

"input type='text' class="form-control" id='datetimepicker5' placeholder="Select to date" value='<?php echo $myDate?>'// $myDate is having value '2016-02-23' "

Note - If You are coding in php then you can set the value of the input datefield using php, but datetimepicker sets it back to current date when you apply datetimepicker() function to it.注意 - 如果您在 php 中编码,那么您可以使用 php 设置输入日期字段的值,但是当您对其应用 datetimepicker() 函数时,datetimepicker 会将其设置回当前日期。 So in order to prevent it, use the given JS code所以为了防止它,使用给定的JS代码

JAVASCRIPT爪哇脚本

<script>
$('#datetimepicker5').datetimepicker({
    useCurrent: false, //this is important as the functions sets the default date value to the current value 
    format: 'YYYY-MM-DD'
});
</script>

在我的版本中,如果您将 val 放入输入中,日历会自动更新。

$('#birthdate').val("01/01/2001");

I wanted to set a date at the startup, and the mentionned methods didn't work.我想在启动时设置一个日期,但提到的方法不起作用。 The date was shown, but when I clicked on the object the date was not selected.显示了日期,但是当我点击对象时,日期没有被选中。

So what I did is retrieving the Datepicker object of the library, and apply the _setDate method on it :所以我所做的是检索库的 Datepicker 对象,并对其应用 _setDate 方法:

$('#dob').data("datepicker")._setDate(date);

But for this to work, the date object must be at midnight on UTC time, so before I did :但要使其正常工作,日期对象必须在 UTC 时间的午夜,所以在我这样做之前:

var date = new Date();
date.setUTCHours(0,0,0,0);

And then it worked.然后它起作用了。

你可以简单地分配一个值(和你一起给你的输入标签,它应该可以工作

<input type="text" class="form-control" value="22-05-2017">

A way of solution :一种解决方法:

<div class="form-group">
  <input type="text" class="form-control datepicker" name="arrivalDate" id="datepicker_arrival">
</div>

So in your js add this :所以在你的 js 中添加这个:

$('.datepicker').datepicker({
   language: 'fr',
   todayHighlight: true,
   orientation: "bottom auto",
   startDate: "today",
   autoclose: true,
});
$('#datepicker_arrival').datepicker('setDate', new Date('2020/04/04'));

I didn't want to fire the changeDate event when I set to default date so I used当我设置为默认日期时,我不想触发 changeDate 事件,所以我使用了

$('#calendarDatePicker').data("date", new Date());

Then instantiate the datepicker然后实例化日期选择器

$('#calendarDatePicker').datepicker();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM