If you are using the bootstrap datetimepicker from http://eonasdan.github.io/ bootstrap-datetimepicker/ Installing/ with knockout and facing issues for mapping the default value this post may help you.
Below is the code I was using for the custom binding.
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
//initialize datepicker with some optional options
var options = {
format:'DD/MM/YYYY hh:mm A',
defaultDate : valueAccessor()(),
sideBySide:true
};
if (allBindingsAccessor() !== undefined) {
if (allBindingsAccessor().datepickerOptions !== undefined) {
options.format = allBindingsAccessor().datepickerOptions.format !== undefined ? allBindingsAccessor().datepickerOptions.format : options.format;
}
}
$(element).datetimepicker(options);
//when a user changes the date, update the view model
ko.utils.registerEventHandler(element, "dp.change", function (event) {
var value = valueAccessor();
if (ko.isObservable(value)) {
value(event.date);
}
});
var defaultVal = $(element).val();
var value = valueAccessor();
value(moment(defaultVal, options.format));
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var thisFormat = 'DD/MM/YYYY hh:mm A';
if (allBindingsAccessor() !== undefined) {
if (allBindingsAccessor().datepickerOptions !== undefined) {
thisFormat = allBindingsAccessor().datepickerOptions.format !== undefined ? allBindingsAccessor().datepickerOptions.format : thisFormat;
}
}
var value = valueAccessor();
var unwrapped = ko.utils.unwrapObservable(value());
if (unwrapped === undefined || unwrapped === null) {
element.value = new moment(new Date());
} else {
element.value = unwrapped.format(thisFormat);
}
}
};
Below is the code I was using for the custom binding.
ko.bindingHandlers.datepicker = {
init: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
//initialize datepicker with some optional options
var options = {
format:'DD/MM/YYYY hh:mm A',
defaultDate : valueAccessor()(),
sideBySide:true
};
if (allBindingsAccessor() !== undefined) {
if (allBindingsAccessor().datepickerOptions !== undefined) {
options.format = allBindingsAccessor().datepickerOptions.format !== undefined ? allBindingsAccessor().datepickerOptions.format : options.format;
}
}
$(element).datetimepicker(options);
//when a user changes the date, update the view model
ko.utils.registerEventHandler(element, "dp.change", function (event) {
var value = valueAccessor();
if (ko.isObservable(value)) {
value(event.date);
}
});
var defaultVal = $(element).val();
var value = valueAccessor();
value(moment(defaultVal, options.format));
},
update: function (element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) {
var thisFormat = 'DD/MM/YYYY hh:mm A';
if (allBindingsAccessor() !== undefined) {
if (allBindingsAccessor().datepickerOptions !== undefined) {
thisFormat = allBindingsAccessor().datepickerOptions.format !== undefined ? allBindingsAccessor().datepickerOptions.format : thisFormat;
}
}
var value = valueAccessor();
var unwrapped = ko.utils.unwrapObservable(value());
if (unwrapped === undefined || unwrapped === null) {
element.value = new moment(new Date());
} else {
element.value = unwrapped.format(thisFormat);
}
}
};
When initiating the model I was passing the values to match the format I have set, but the datetimepicker was returning NaN exception when the date value is more than 12 and then I got to know option parameter format passed when initializing the datetimepicker was only used for formatting the input text and when reading from the input box the defaultDate option should have a valid date which is understandable by moment.js. So for resolving the issue the other option I found is when initializing the observable can pass the moment object and moment object can be initialized with the any given format when format is passed. So here below is the code to initialize the observable which is used with the datetimepicker binding.
function TestModel(data){
self.test_date = ko.observable(moment().add('1','week'));
if(data.test_date != undefined){
isNumeric = /^(\d+)$/;
//if data comes from mySQL then use unix timestamp
//or if data comes from posted value when reloading use custom format
if(isNumeric.test(data.test_date )){
self.whencantake_date(moment.unix(data.test_date ));
} else{
self.whencantake_date(moment(data.test_date ,'DD/MM/YYYY hh:mm A'));
}
}
}
Hope this helped you.