angular - 如何在 Angular 和 TypeScript 中格式化日期?
问题描述
我有一个显示日期的表格。
我的表格同时显示日期和时间,但我只想显示日期。
这是我的代码:
import {DatePipe, formatDate} from '@angular/common';
import * as moment from 'moment/moment';
export class RegistrationModel{
firstName: string;
lastName: string;
middleName: string;
nickname: string;
createdAt: string;
applicantCV: string;
mobileNumber: string;
emailAddress: string;
datePipe = new DatePipe('en-US');
constructor(response){
//formatDate(this.createdAt, 'M/d/yy', 'en-US');
this.firstName = response.firstName;
this.lastName = response.lastName;
this.middleName = response.middleName;
this.nickname = response.personalData.nickname;
this.createdAt = response.createdAt;
this.datePipe.transform(this.createdAt, 'M/d/yy');
this.applicantCV = response.applicantCV;
this.mobileNumber = response.contactInfo.mobileNumber;
this.emailAddress = response.contactInfo.emailAddress;
}
}
解决方案
我猜您在转换日期后忘记重新分配createdAt
变量。
this.createdAt = this.datePipe.transform(new Date(this.createdAt), 'M/d/yy');
编辑
您似乎还必须先将 createdAt 转换为 Date。
推荐阅读
- php - 无法连接到服务器需要最新答案所有其他帖子都非常旧并且无法正常工作
- python - 使用函数来模拟类的方法返回值
- python - 如何以稳定的方式使用 pytest 断言列表?
- markdown - GitHub Markdown 中的表格和图像并排
- python - 使用python构建杂乱数据
- selenium - [Ashot][Selenium] - 为 iOS 和 Android 截屏
- python - 如果将 QThread 创建为局部变量,为什么 QThread 的行为会有所不同
- gatsby - netlify-cms git 提交未显示在网站上
- mysql - 需要有关查询性能的帮助
- python - 将 Python 程序设置为在一定天数后过期