title: Display and Retrieve date using knockout js
body: <p>I just wanted to display and retrieve date using knockout JS. The following are my JSON and html code:</p>
  
  <pre><code>{"PropertyID":1,"ExpenseTypeID":1,"ExpenseDate":"\/Date(1386638350997)\/","Amount":234.4,"Property":null,"ExpenseType":null,"ID":1,"CreateDate":null,"CreatedBy":null,"UpdateDate":null,"UpdatedBy":null}
  </code></pre>
  
  <p>The HTML:</p>
  
  <pre><code>&lt;input id="StartDate" data-bind="datepicker: ExpenseDate" /&gt;
  </code></pre>
  
  <p>ViewModel</p>
  
  <pre><code> 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;
              observable($el.datepicker("getDate"));
          });
  
          //handle disposal (if KO removes by the template binding)
          ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
              $el.datepicker("destroy");
          });
      },
      update: function (element, valueAccessor) {
          var value = ko.utils.unwrapObservable(valueAccessor()),
              $el = $(element),
              current = $el.datepicker("getDate");
          if (value - current !== 0) {
              $el.datepicker("setDate", value);
          }
      }
  };
  </code></pre>
  
  <p>I changed the line of code </p>
  
  <pre><code>var observable = valueAccessor() to var observable = valueAccessor;
  </code></pre>
  
  <p>because otherwise I got one problem when i wanted to set date from datepicker. The error was</p>
  
  <p>TypeError: observable is not a function observable($el.datepicker("getDate"));</p>
  
  <p>By changing this line of code I can set date but can't retrieve it when I want to save data.
  I also tried using <code>self.SelectedDate = ko.observable("");</code> and   </p>
  
  <pre><code>&lt;input id="StartDate" data-bind="value: $root.SelectedDate" /&gt; 
  </code></pre>
  
  <p>But no luck.Can any one help how can I do it.</p>
  
  <p>Thanks,</p>