Abel Valdez
Abel Valdez

Reputation: 2408

Sort formating date in DataTable Angular

I have my API and I get it using HTTP GET I cast my info with my model Alert taht contains a Date type timestamp if I run the DataTable with a Date type I can sort the column, but the problem here is the format I'm getting the following format

Mon Sep 03 2018 01:56:36 GMT-0700 (Pacific Daylight Time (Mexico))

When I try to have a formated date like 09/03/2018 01:56:36 If I run the datatable with this format the sort doesn't work because it sort as String and not as Date

My question is: How can I have a Date Type formated as MM/DD/YYYY hh:mm:ss ??

Note: I'm using ng2-smart-table as Datable

Upvotes: 3

Views: 3492

Answers (2)

Andreq Frenkel
Andreq Frenkel

Reputation: 1208

as per documentation

You can declare pre-render function valuePrepareFunction for any field. So, you can import DatePipe directly:

import { DatePipe } from '@angular/common';

and return new DatePipe('en-US').transform(date, 'your-format-here'); inside valuePrepareFunction declaration

Upvotes: 5

Bruno João
Bruno João

Reputation: 5545

Do not format the date. Let it as timestamp and use the valuePrepareFunction to transform the timestamp in your formatted date.

If it is already what you are doing so I can see that the sort uses the prepared value and not the original one.

In this case, use the compareFunction to transform the date in timestamp again when sorting.

https://akveo.github.io/ng2-smart-table/#/documentation

Upvotes: 2

Related Questions