Ruslan
Ruslan

Reputation: 10147

Knockout.js input type date data binding in Google Chrome

I'm struggling to set a value on the input of type="date" in google chrome: http://jsfiddle.net/ruslans/gNv7H/

<input data-bind="value: dateString" type="date"></input>

var viewModel = {
    someDate: new Date(parseInt("/Date(1367708400000)/".substr(6)))
};
ko.applyBindings(viewModel);

My date will come from JSON data but first I need to find out which format does it need to be in for Chrome's date picker to recognize the binding. Would I have to do it with jQuery selector and set .val() on the field? Seems daft...

Edit: according to this article, the date format to set the value on Google date input must always be "yyyy-mm-dd". Which is a pitty, because we're using jQuery date picker for all browsers where there's no native date pickers exist.

Upvotes: 10

Views: 6882

Answers (1)

nemesv
nemesv

Reputation: 139788

You just need to correctly format your value as described in the W3C working draft:

A valid full-date as defined in RFC 3339, with the additional qualification that the year component is four or more digits representing a number greater than 0.

Example: 1996-12-19

So the following should work:

var viewModel = {    
    dateString: ko.observable('2002-02-02')
};

Demo JSFiddle.

Upvotes: 12

Related Questions