In my view I wish to display a knockout.js
binded field that contains a date. It is just a display field and not an input field. Something like below when basemodel.actionDate = ko.observable()
<p class="display-field" data-bind="text: baseModel.actionDate"/>
However this is displayed as follows:
2013-06-17T11:56:18.4537687Z
What is the easiest way to format this dd mm yyyy
. eg: 17 June 2013
?
If you are referencing moment.js then I would actually format in the knockout model.
I had some issues (I think) with the mapping plugin trying to use the extender. Since I'm only displaying dates and not allowing them to be edited I prefer to just use a binding handler like this:
Here's the handler:
I chose to add a prototype to Date object like this (and call
toShortDateString
on the Date object created in the handler)- but you can replace the logic above withGlobalize
or whatever you prefer.Either make sure the service output it in a correct format, or format in client side
If you want todo it client side then you can parse the ISO date string into a Date object and then use jQuery globalize to format it to desired format.
I use KO extenders for this
http://jsfiddle.net/vRf5B/42/
update
I got a question on my blog how to retrieve the raw date value It can be done my exposing the raw value on the computed like
http://jsfiddle.net/vRf5B/91/
Declare format function:
And use it with the date strings as:
I recommend the moment.js date formatting library.
Using it, you can do something like this in your view: