jjzjj

javascript - Bootstrap 3 Datepicker v4 - 设置默认日期

coder 2025-03-24 原文

我正在使用这个日期时间选择器 http://eonasdan.github.io/bootstrap-datetimepicker/在我的编辑表单中。我无法根据我的可变日期在此日期时间选择器中设置默认值。如果我在输入元素上使用 $('#edit_cost_date').val(json[0]['date']);,输入中的值是正确的,但如果我打开 datetimepicker,我会看到标记不是输入的日期而是实际日期。

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

最佳答案

您确定您的 date 变量是 Date 对象吗?如果不是,您需要像这样设置默认日期:

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

有关该插件的 date 属性的更多信息:

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

date([newDate])

Takes newDate string, Date, moment, null parameter and sets the components model current moment to it. 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.

Throws

TypeError - in case the newDate cannot be parsed

Emits

change.dp - In case newDate is different from current moment

关于javascript - Bootstrap 3 Datepicker v4 - 设置默认日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28795079/

有关javascript - Bootstrap 3 Datepicker v4 - 设置默认日期的更多相关文章

随机推荐