Using Knockout to databind a date to a date picker and time picker

Here is just a basic proof of concept I worked up for a project where we wanted to use an HTML5 timeselector, which requires the time values to be formatted in a specific way, and also deals with some of the odd Daylight Savings Time issues based on the date component.

This example requires jquery, knockout and moment.

http://jsfiddle.net/NAgNV/2429/

<input data-bind=”datepicker: myDate” />
<input data-bind=”timepicker: myDate” type=”time” />
<hr />
<button data-bind=”click: setToCurrentDate”>Set To Current Date</button>
<button data-bind=”click: showCurrentDateTime”>Show current date</button>
<hr />

———-

ko.bindingHandlers.datepicker = {
     init: function (element, valueAccessor, allBindingsAccessor) {
   
         var $el = $(element);

        //initialize datepicker with some optional options
         var options = allBindingsAccessor().datepickerOptions || {};
         $el.datepicker(options);

        //handle the field changing
         ko.utils.registerEventHandler(element, “change”, function () {
            
             var observable = valueAccessor();
             var oldDate = observable();
               var newDate = $el.datepicker(“getDate”);
             var newDate = new Date(newDate.toLocaleDateString() + ” ” + oldDate.toLocaleTimeString());
            
             observable(newDate);
         });

        //handle disposal (if KO removes by the template binding)
         ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
             $el.datepicker(“destroy”);
         });

    },
     update: function (element, valueAccessor) {
     var d = viewModel.myDate();
     if (d!=null) alert(d);
         var value = ko.utils.unwrapObservable(valueAccessor()),
             $el = $(element),
             current = $el.datepicker(“getDate”);
                
         if (value – current !== 0) {
            
             $el.datepicker(“setDate”, value);
         }
     }
};

ko.bindingHandlers.timepicker = {
     init: function (element, valueAccessor, allBindingsAccessor) {
         // Get the value of the value binding
         var value = valueAccessor();
         var thisElement = $(element);
         console.log(‘The value of the element should be here – ‘, value());
         // If the value is not a valid moment in time,
         console.log(!moment(value()).isValid());
         //handle the field changing
         ko.utils.registerEventHandler(element, “change”, function () {
             var observable = valueAccessor();
             console.log(‘observable – ‘, observable());
         var tzdstOffset = (observable().getTimezoneOffset() + (moment(observable()).isDST() ? 60 : 0));
         var adjustedTime = new Date($(element)[0].valueAsDate.getTime() + (tzdstOffset * 60 * 1000));
             var adjustedDate = observable();
             adjustedDate.setHours(
                 adjustedTime.getHours(),
                 adjustedTime.getMinutes(),
                 adjustedTime.getSeconds()
             );
             observable(adjustedDate);
         });
     },
     update: function (element, valueAccessor, allBindingsAccessor) {
         var value = ko.utils.unwrapObservable(valueAccessor());
         var thisElement = $(element);
         console.log(‘Value updating to -‘, value);
         var thisMoment = new moment(value).format(‘HH:mm’);
         //console.log(‘Value updating to -‘, thisMoment);
         console.log(‘This is the element – ‘, thisMoment);
         thisElement[0].value = thisMoment;
         console.log(‘This is the element – ‘, thisElement);
     }
}

function showCurrentDateTime()
{
     alert(viewModel.myDate().toLocaleString());
}

var viewModel = {
     //myDate: ko.observable(new Date(“12/01/2013 8:00 AM”)),
     myDate: ko.observable(new Date(“2017-08-09T04:23:30.8295065-02:00”)),
     setToCurrentDate: function () {
         this.myDate(new Date());
     }
};

ko.applyBindings(viewModel);

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s