我在Knockout.js视图模型中使用Bootstrap datatimepicker。这是设置:
ko.bindingHandlers.dateTimePicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().dateTimePickerOptions || {};
options['format'] = options['format'] || "dddd, Do MMMM YYYY, HH:mm"; //default format
options.ignoreReadonly = true;
options.defaultDate = valueAccessor()();
$(element).datetimepicker(options);
//when a user changes the date, update the view model
ko.utils.registerEventHandler(element, "dp.change", function (event) {
ignoreReadonly: true;
var value = valueAccessor();
if (ko.isObservable(value)) {
if (event.date !== null && !(event.date instanceof Date)) {
value(event.date.toDate());
} else {
value(event.date);
}
}
});
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
var picker = $(element).data("DateTimePicker");
if (picker) {
picker.destroy();
}
});
},
update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var picker = $(element).data("DateTimePicker");
//when the view model is updated, update the widget
if (picker) {
var koDate = ko.utils.unwrapObservable(valueAccessor());
//in case return from server datetime i am get in this form for example /Date(93989393)/ then fomat this
koDate = (typeof (koDate) !== 'object') ? new Date(parseFloat(koDate.replace(/[^0-9]/g, ''))) : koDate;
picker.date(koDate);
}
}
};
如果datetimepicker读取日期时间值,其中分钟,秒或毫秒为“ 0”,则它将年值有些奇怪。
这里是一个例子。在这种情况下,秒和毫秒均为“ 0”:
数据库值为:2018-06-23 12:49: 00.0000000 ;
初始 的Json视图模型值为:2018-06-23T12:49:00;
视图模型值 之后,我的datetimepicker已“初始化” 值(请注意奇怪的年份值“ 2609”):Sun Jul 02 2609 :格林尼治标准时间+0100(英国夏令时);和
该控件的呈现方式如下:
如果任何或所有分钟,秒或毫秒为“ 0”,则会发生相同的行为。
相反,例如,2018-06-23 14:08: 05.9288927 不会表现出这种行为:
我用于“初始化”或读取datetimepicker值的代码在做一些奇怪的事情。但是我无法修复它。有人可以帮忙吗?