How do you set the default value of a jQuery DatePicker that is bound with KnockoutJS?

http://stackoverflow.com/questions/6694248/how-do-you-set-the-default-value-of-a-jquery-datepicker-that-is-bound-with-knocko

by rniemeyer

HTML

<script src="https://github.com/jquery/jquery-tmpl/raw/master/jquery.tmpl.js"></script>
<script src="http://knockoutjs.com/downloads/knockout-2.2.1.js"></script>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.11/themes/base/jquery-ui.css">
<span data-bind="text: myDateObject"></span>
<input style="display:none" 
data-bind="datepicker: myDateObject, 
           datepickerOptions: { 
              buttonImage: 'http://www.gravatar.com/avatar/235fa10da0d3543a0a1f37c8b71575cc?s=32&amp;d=identicon&amp;r=PG', 
              buttonImageOnly: true, 
              showOn: 'button', 
              showOtherMonths: true,
              selectOtherMonths: true,
              defaultDate: new Date(), 
              minDate: '-1m',
              maxDate: '+1m'  }" />

CSS

td, th { padding: 5px; border: solid 1px black; }
th { color: #666; }
a { font-size: .75em; }

JavaScript

ko.bindingHandlers.datepicker = {
    init: function(element, valueAccessor, allBindingsAccessor) {
        //initialize datepicker with some optional options
        var options = allBindingsAccessor().datepickerOptions || {};
        $(element).datepicker(options);
          
        //handle the field changing
        ko.utils.registerEventHandler(element, "change", function () {
            var observable = valueAccessor();
            observable($(element).datepicker("getDate"));
        });
    
    },
    update: function(element, valueAccessor) {
        var value = ko.utils.unwrapObservable(valueAccessor());
        $(element).datepicker("setDate", value);        
    }
};

var viewModel = {
    myDateObject: ko.observable(),
};

ko.applyBindings(viewModel);