Reputation: 1669
Im implementing Ag Gird in Angular 2. I have a date column and sorting of dates and Ag grid expects type of date in the format only. Thus I had to send it in the form of date for rendering. But my input is string. I have my dateString in the format 2017-01-19. I'm doing
let myDate:Date = moment(dateString).toDate();
But it's givimg me output as Thu Jan 19 2017 00:00 Indian Standard Time. I tried like this too
let myDate:Date = moment(dateString).format("DD/MM/YYYY");
This gives me error- string is not assignable to Date.
Tried like this too
let myDate:Date = moment(dateString,"DD/MM/YYYY");
But no luck. What I want is, type of the output as date and in the form 19-01-2017
Please suggest
Upvotes: 13
Views: 57131
Reputation: 321
moment().toDate();
gives a Date object
got the answer from Moment.js transform to date object
Upvotes: 5
Reputation: 3099
You should look to utilise a column renderer framework in your column definitions. Where the field date
should be a javascript date. You can generate this from a string via new Date("2017.02.28")
, there is no need here to use moment.
{
colId: "date",
headerName: "Date",
field: "date",
cellRendererFramework: GridDateComponent,
}
This component displays the date formatted how you want utilising the date
pipe, whilst preserving the original JSDate for sorting etc.
import { Component } from '@angular/core';
import { AgRendererComponent } from 'ag-grid-ng2/main';
@Component({
selector: 'date-cell',
template: `{{params.value | date:'d MMM'}}`
})
export class GridDateComponent implements AgRendererComponent {
public params: any;
agInit(params: any): void {
this.params = params;
}
}
Upvotes: 0
Reputation: 293
Try this:
let myDate:Date = moment(dateString,"YYYY-MM-DD").format("DD-MM-YYYY");
Basically, when you call moment()
, you should specify the format your current date is in, not the output format that you want. The output format should be specified when calling .format()
.
Upvotes: 15