創建日期選擇淘汰賽custombinding將允許此日期格式。
修訂此答案(固定「CHANGEDATE」錯誤並加入可觀察的setter)https://stackoverflow.com/a/6613255/712700,以下將做:
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor) {
//initialize datepicker with some optional options
var options = allBindingsAccessor().datepickerOptions || {},
$el = $(element);
$el.datepicker(options);
//handle the field changing by registering datepicker's changeDate event
ko.utils.registerEventHandler(element, "change", function() {
var observable = valueAccessor();
observable($el.datepicker("getDate"));
});
//handle disposal (if KO removes by the template binding)
ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
$el.datepicker("destroy");
});
var dateValue = $el && $el.length > 0 ? $el[0].value : undefined;
if (dateValue) {
valueAccessor()(dateValue);
}
},
update: function (element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor()),
$el = $(element);
//handle date data coming via json from Microsoft
if (String(value).indexOf('/Date(') == 0) {
value = new Date(parseInt(value.replace(/\/Date\((.*?)\)\//gi, "$1")));
}
var current = $el.datepicker("getDate");
if (value - current !== 0) {
$el.datepicker("setDate", value);
}
var dateValue = $el && $el.length > 0 ? $el[0].value : undefined;
if (dateValue) {
valueAccessor()(dateValue);
}
}
};
設定所述敲除可觀察到的部分是這樣的:
var dateValue = $el && $el.length > 0 ? $el[0].value : undefined;
if (dateValue) {
valueAccessor()(dateValue);
}
還要注意,如果你使用的是C#.Net和你有一些日期從後端到JS土地進來的(最初),你可以在那時格式化你的日期,因此不會成爲一個JavaScript的,而是它將是後端將會友好的一個接受,如果你碰巧把它傳回去。這是你如何做牛頓軟件:
var backendSerialisedModel = @Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model, new Newtonsoft.Json.Converters.IsoDateTimeConverter() { DateTimeFormat = "dd-MM-yyyy" }));