Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knockout - how to bind date only

I have an array, one key is createDate. In a foreach looping of the array, I pull the createDate value via a knockout text binding.

<span data-bind="text: createDate"></span>

The value displays: '2013-04-24T16:29:00.38' ... this is the way it exists in the database. Is there a way to format (within the binding) to show just the date only? Or do I have to save it as date only in the database to achieve this?

E.g., you can use the following to set 2 decimals:

<span data-bind="text: price.ToFixed(2)"></span>

Is there something simple such as this to only display: '2013-04-24'

Thanks in advance !

like image 762
nanonerd Avatar asked May 08 '13 00:05

nanonerd


1 Answers

For me the key is moment.js

You can do something as simple as this:

<span data-bind="text: moment(createDate()).format('MM/DD/YYYY')"></span>

Or you could write a binding helper like this guy does, so it would be something like:

<span data-bind="dateString: createDate, datePattern: 'MM/DD/YYYY'"></span>
like image 130
Jason Haley Avatar answered Sep 26 '22 06:09

Jason Haley