5

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.

2
  • I'm not familiar with devexpress at all, but couldn't you perhaps use the angular date pipe for this: [dataField]="receiptDate | date:'short'"? Commented May 18, 2017 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 Commented May 18, 2017 at 15:03

5 Answers 5

10

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>
Sign up to request clarification or add additional context in comments.

Comments

9

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>

Comments

5
  • 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>

Comments

1

Going the extra mile:

In case you want the filtering (search) input in your DevExpress grid to match the format specified in the format option of a dxi-column with a dataType of "date", you can use the useMaskBehavior option.

HTML Example:

<dxi-column dataField="dateTime" dataType="date" format="dd/MM/yyyy"
           caption="Date"
           [allowEditing]="false"
           [editorOptions]="{ useMaskBehavior: true }">
</dxi-column>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.