admin管理员组文章数量:1310498
I have a mat-table which displays columns Date, Before Time Period and After Time Period. Code for HTML :-
<ng-container
matColumnDef="{{ column }}"
*ngFor="let column of columnsToDisplay"
>
<th mat-header-cell *matHeaderCellDef>{{ column }}</th>
<td mat-cell *matCellDef="let element">
{{ element[column] }}
</td>
</ng-container>
TypeScript Code:
columnsToDisplay = [
'executionDate',
'previousTimePeriod',
'afterTimePeriod'
];
executionDate: string = new Date().toISOString().slice(0, 10);
If I use a pipe {{ element[column] | date: 'yyyy-MM-dd'}}
to display Date of type string to date type then I am not able to see Before and After Time Period.
How can I view date only as 'yyyy-MM-dd'
I have a mat-table which displays columns Date, Before Time Period and After Time Period. Code for HTML :-
<ng-container
matColumnDef="{{ column }}"
*ngFor="let column of columnsToDisplay"
>
<th mat-header-cell *matHeaderCellDef>{{ column }}</th>
<td mat-cell *matCellDef="let element">
{{ element[column] }}
</td>
</ng-container>
TypeScript Code:
columnsToDisplay = [
'executionDate',
'previousTimePeriod',
'afterTimePeriod'
];
executionDate: string = new Date().toISOString().slice(0, 10);
If I use a pipe {{ element[column] | date: 'yyyy-MM-dd'}}
to display Date of type string to date type then I am not able to see Before and After Time Period.
How can I view date only as 'yyyy-MM-dd'
- Please provide some sample data to work with. Also, it would be really helpful if you could provide a sample stackblitz replicating your issue. – SiddAjmera Commented Feb 12, 2019 at 7:45
3 Answers
Reset to default 4create a pipe:
import { Pipe, PipeTransform } from '@angular/core';
import * as moment from 'moment';
@Pipe({
name: 'momentPipe',
pure: false
})
export class MomentPipe implements PipeTransform {
constructor() { }
transform(value: string, dateFormat: string): any {
return moment(value).format(dateFormat);
}
}
then in html:
<span >{{ element[column] | momentPipe: 'dddd D MMM YYYY' }}</span>
You need moment for this approach see here more formats
My suggestion is to use angular momentjs
So it'll be simple that you can use its filter as
{{ element[column] | amParse: 'YYYY-MM-DDTHH:mm:ss.SSSSZ' | amDateFormat: 'yyyy-MM-dd' }}
Another idea is you can convert your string-date into 'yyyy-MM-dd' format and replace with replacing with string-date.
This link should help you.
本文标签: javascriptHow to convert string to type date in dynamically generated mattableStack Overflow
版权声明:本文标题:javascript - How to convert string to type date in dynamically generated mat-table - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741819327a2399268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论