Search code examples
knockout.jsmomentjskogrid

Formatting dates in koGrid using Moment.Js


I have a grid that has a JSON date field. I want to format the field using Moment.JS but can't get the cellFormatter nor cellFilter fields to work.

See the following fiddle: http://jsfiddle.net/cooper/8NNAk/5/ that shows the problem.

I want to apply the formatting to the HTML / data-grid, not the data, for example I don't want to do this:

function viewModel(){
    myArray = ko.observableArray([
        { MyDate: moment("/Date(1355875200000+0000)/").format('DD/MM/YYYY') },
        { MyDate: moment("/Date(1355875300000+0000)/"}.format('DD/MM/YYYY')} ]);
}

Update:
This fiddle doesn't appear to be working in IE.


Solution

  • It should work with the following cellFilter function:

    cellFilter: function(data) { return moment(data).format('DD/MM/YYYY') }
    

    Here is how the full columndef looks like:

    columnDefs: 
        [
            { field: 'MyDate', displayName: 'DateTime', 
            cellFilter: function(data) { return moment(data).format('DD/MM/YYYY') } }
        ]
    

    Demo JSFiddle.