-1

Knockout js binder for twitter bootstrap timepicker.

click here for timepicker plugin

Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35

1 Answers1

0

We are converting a moment js object to a formatted time string but you can remove the code where we are converting and use it.

$(function () {
    ko.bindingHandlers.timePickerMoment = {
        init: function (element, valueAccessor, allBindingsAccessor) {
            //initialize timepicker with some optional options
            var options = allBindingsAccessor().timePickerOptions || {};
            var valueOfElement = ko.unwrap(valueAccessor());
            $(element).val(valueOfElement.format("HH:mm")); //Converting moment object to HH:mm
            $(element).timepicker(options);

            //when a user changes the date, update the view model
            ko.utils.registerEventHandler(element, "changeTime.timepicker", function (event) {
                var value = valueAccessor();
                if (ko.isObservable(value)) {   
                    value(moment(event.time.value, 'HH:mm')); //Converting HH:mm to moment
                }
            });
        },
        update: function (element, valueAccessor) {
            var widget = $(element).val();
            //when the view model is updated, update the widget
            if (widget) {
                var date = ko.utils.unwrapObservable(valueAccessor());
                $(element).val(date.format("HH:mm")); //Converting moment object to HH:mm
            }
        }
    };
});

And this is how it is used.

data-bind="dateTimePickerMoment: OpeningTimeMoment, dateTimePickerOptions:{ locale: 'en-GB', format: 'HH:mm', showClose: true }, asString: true"
Venkata K. C. Tata
  • 5,539
  • 4
  • 22
  • 35