3

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

2 Answers 2

5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks you you answer It helps me a lot.
is this working? I have this format date "2019-10-01T08:52:45.563" and and format the date using DatePipe and return it inside the valuePrepareFunction but the sorting still not working.
@AbelValdez is this correct? ` lastLoginTime: { title: 'Last Login', width: '15%', filter: false, valuePrepareFunction: (lastLoginTime: any) => { return lastLoginTime? new DatePipe('en-US').transform(lastLoginTime, 'M/d/yyyy') : new DatePipe('en-US').transform('2019-12-03T08:52:45.563', 'd/M/yyyy'); } }, `
2

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

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.