简体   繁体   English

Bootstrap 3 Datepicker v4 - 设置默认日期

[英]Bootstrap 3 Datepicker v4 - set default date

im using this datetimepicker http://eonasdan.github.io/bootstrap-datetimepicker/ in my edit form. 我在我的编辑表单中使用这个datetimepicker http://eonasdan.github.io/bootstrap-datetimepicker/ Im not able to set up default value in this datetimepicker from my variable date. 我无法从我的变量日期在此datetimepicker中设置默认值。 If I use $('#edit_cost_date').val(json[0]['date']); 如果我使用$('#edit_cost_date').val(json[0]['date']); on input element, the value in input is right but if i open datetimepicker i see that marked is not the date in input but actual date. 在输入元素上,输入中的值是正确的,但如果我打开datetimepicker,我看到标记的不是输入中的日期而是实际日期。

var date = json[0]['date'];
$(function () {
    $('#datetimepicker-edit-cost').datetimepicker({
        locale: 'cs',
        format:'DD.MM.YYYY'
    });
});

Are you sure your date variable is a Date object? 您确定您的date变量是Date对象吗? If it is not, you will need to set the default date like this: 如果不是,您需要设置默认日期,如下所示:

var date = json[0]['date'];
$(function () {
    $('#datetimepicker-edit-cost').datetimepicker({
        locale: 'cs',
        format:'DD.MM.YYYY',
        defaultDate: new Date(date)
    });
});

More information about the date property for that plugin: 有关该插件的date属性的更多信息:

http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#date http://eonasdan.github.io/bootstrap-datetimepicker/Functions/#date

date([newDate]) 日期([newDate])

Takes newDate string, Date, moment, null parameter and sets the components model current moment to it. 获取newDate字符串,Date,moment,null参数并设置组件模型当前时刻。 Passing a null value unsets the components model current moment. 传递空值会取消组件模型当前时刻。 Parsing of the newDate parameter is made using moment library with the options.format and options.useStrict components configuration. 使用包含options.format和options.useStrict组件配置的时刻库来解析newDate参数。

Throws 抛出

TypeError - in case the newDate cannot be parsed TypeError - 如果无法解析newDate

Emits 发射

change.dp - In case newDate is different from current moment change.dp - 如果newDate与当前时刻不同

As also mentioned in the answer by John Washam, the documentation clearly says that the date function accepts a moment object, among others. 正如John Washam的回答中所提到的, 文档明确指出date函数接受一个moment对象等等。
I was in the same situation, trying various things, but nothing seemed to work, except for this idea: 我处于相同的情况,尝试各种各样的事情,但除了这个想法,似乎没有任何工作:
Why not feed a moment object to the date function? 为什么不将moment对象提供给date函数?

var date = json[0]['date'];
$(function () {
   $('#datetimepicker-edit-cost').datetimepicker({
      locale: 'cs',
      format:'DD.MM.YYYY'
   });
   $('#datetimepicker-edit-cost').data("DateTimePicker").date(moment(date));
});

Since the moment.js library is required for the datepicker to work, you can use it to parse any string , and directly feed the date function with its result. 由于datepicker库需要moment.js库,因此您可以使用它来解析任何字符串 ,并直接使用其结果提供日期函数。

this works for me 这对我有用

 var date = new Date('11/1/2013');
or  var date2 = '11/1/2013';


        $('#datetimepicker1').datetimepicker({
            locale: 'cs',
            format:'DD.MM.YYYY',
            defaultDate: date

        });

Its documentation defaultDate value Accepts: date, moment, string 文档 defaultDate值接受:日期,时刻,字符串

There are some real bugaboos in setting up this tempermental little control. 设置这个温和的小控件有一些真正的bugaboos。 First, remove any ngModal binding expressions on the input controls. 首先,删除输入控件上的任何ngModal绑定表达式。 These will blow up your attempts to set and show the default value. 这些会破坏您设置和显示默认值的尝试。 Instead, use @ViewChild to get at the value when you need it. 而是使用@ViewChild在需要时获取值。 Second, if your control is going to be in a modal popup component that's not visible during the initial load, you may run into more issues. 其次,如果您的控件将位于初始加载期间不可见的模态弹出组件中,则可能会遇到更多问题。 I could never get the element.data("DateTimePicker") / element.data("datetimepicker") to ever initialize or become available prior to displaying the control. 在显示控件之前,我永远无法使element.data(“DateTimePicker”)/ element.data(“datetimepicker”)初始化或变得可用。 It's simply not there. 它根本就不存在。 Third, set the locale. 第三,设置区域设置。 This seems to help a ton with the initial display regardless of the format. 无论格式如何,这似乎有助于初始显示。 (Maybe I'm just superstitious. Getting this to work seemed like voodoo :) (也许我只是迷信。让它发挥作用似乎是伏都教:)

In the end, I set up a proper TypeScript import with my own datepicker.d.ts and datepicker.js files that contained functions for initializing a control. 最后,我使用自己的datepicker.d.ts和datepicker.js文件设置了一个正确的TypeScript导入,其中包含初始化控件的函数。 My function is as follows in my datepicker.js: 我的函数在我的datepicker.js中如下:

export function datepickerinit(t,d){
    if(!d)
        d=new Date();
    var m=moment(d).format('MM/DD/YYYY');
    var j=$('#'+t);
    if(j.data('DateTimePicker')){
        j.data('DateTimePicker').date(m); //this seems to never be true
    } 
    else {
        j.datetimepicker({
            date:m,
            defaultDate:m,
            format: 'MM/DD/YYYY',
            locale:'en-US',
            viewDate:m
        });
    }
}

I end up with a the date picker displaying my default value before the user clicks. 我最终得到一个日期选择器,在用户点击之前显示我的默认值。 (When it wasn't working, I'd end up with a fully expanded GMT datetime string before the user clicked the picker.) (当它不工作时,我会在用户点击选择器之前使用完全展开的GMT日期时间字符串。)

When my users click a button to do an action, I get the value from my ViewChild's nativeElement.value property. 当我的用户单击按钮执行操作时,我从ViewChild的nativeElement.value属性中获取值。

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

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