3

I have a devextreme datagrid working in my angular2 project. However a column with a date datatype just shows the full javascript date in this format: yyyy-MM-ddTHH:mm:ss. I would like to set the format for this column to something more user friendly like dd.MM.yyyy (european style).

But all my attempts to set the format of the date were not successful until now.

Below is what I have in the component template until now:

    <dx-data-grid [dataSource]="tasks">
        <dxi-column dataField="barcode" ></dxi-column>
        <dxi-column dataField="receiptDate" format="shortDate">
            <dxo-format type="shortDate"></dxo-format> 
        </dxi-column>
    </dx-data-grid>

Unfortunately setting the type of the format doesn't change anything at all - I still get this unformated JS date string. And I also don't get any exception in the console.

marco birchler
  • 1,566
  • 2
  • 21
  • 45
  • I'm not familiar with devexpress at all, but couldn't you perhaps use the [angular date pipe](https://angular.io/docs/ts/latest/api/common/index/DatePipe-pipe.html) for this: `[dataField]="receiptDate | date:'short'"`? – Michael Doye May 18 '17 at 13:13
  • 1
    @und3rTow thank you for your input - unfortunately at this point the pipe is not valid - but I have found the solution meanwhile – marco birchler May 18 '17 at 15:03

4 Answers4

7

I found the answer myself after a while. The problem is that you must set the dataType property of the column to date - otherwise the date formating setting will be ignored. This is working:

<dx-data-grid [dataSource]="tasks">
    <dxi-column dataField="barcode" ></dxi-column>
    <dxi-column dataField="receiptDate" dataType="date" format="shortDate"></dxi-column>
</dx-data-grid>
marco birchler
  • 1,566
  • 2
  • 21
  • 45
6

The answer mentioned above works fine, if you want to use pipes ( may be for some addditional formatting or anything ) you can use cellTemplate , below is the code snippet

    <dx-data-grid [dataSource]="assessments" [height]="gridHeight" [hoverStateEnabled]="true" 
                width="100%">
                <dxi-column dataField="assessDate" dataType="date" [caption]="assessDate" cellTemplate="dateCell"></dxi-column>
                <div *dxTemplate="let cellData of 'dateCell'">
                    {{cellData.value | date: 'dd-MMM-yy'}}
                </div>
            </dx-data-grid>
pritesh agrawal
  • 1,155
  • 8
  • 16
4
  • once you modified dataType="date", then devexpress components are smart enough to catch format from your custom angular pipe if you passed as a format format="myFormatPipe .
  • or you can simply do this you can do this:
<dxi-column
  dataField="receiptDate"
  dataType="date"
  format="dd/MM/yyyy"
></dxi-column>

Muhammed Moussa
  • 4,589
  • 33
  • 27
0

It was showing 1 day before. Below changes worked for me format=dd-MM-yyyy" To [format]="{ type: 'dd-MM-yyyy' }"

Shahid Hussain Abbasi
  • 2,508
  • 16
  • 10