gpt4 book ai didi

react 形式的 Angular 6 日期格式 MM/dd/yyyy

转载 作者:太空狗 更新时间:2023-10-29 17:58:27 24 4
gpt4 key购买 nike

我正在使用具有反应形式的 Angular 6,我正在尝试弄清楚如何格式化从 webapi 返回的日期值。日期显示为 1973-10-10T00:00:00,我想将其格式化为 10/10/1973。下面是检索数据并显示它的代码。 webapi json dob 值为 1973-10-10T00:00:00。模型中的dob值是Date类型。

html

<input formControlName="dob" type="text" [value]="dob | date: 'MM/dd/yyyy'" class="form-control" />

绑定(bind)形式

this.userInfoForm = this.fb.group({
'userId': ['', [Validators.required]],
'dob': ['', [Validators.required]]
});

加载保存

this.as.accountUserInfo(this.activeRoute$.ActiveUserId).subscribe(data => {
this.userInfoModel$ = data as UserInfoModel;
this.userInfoForm.patchValue(this.userInfoModel$);
});

API 调用

accountUserInfo(userId: number) {
return this.http.post(this.url + "UserInfo", userId)
.pipe(
retry(3),
catchError(this.handleError)
)}

对于如何将日期转换为 MM/dd/yyyy 格式的任何帮助或指导,我深表感谢。将 html 设置为日期值确实使它看起来不错,但我不想使用内置的浏览器日期显示,因此我需要将其转换为字符串。

提前致谢。

最佳答案

使用日期选择器时遇到同样的问题。解决方案很简单:将输入类型更改为“日期”

之前:

<input type="text" class="form-control" placeholder="dd/mm/yyyy"
formControlName="inputNascimento" ngbDatepicker #d="ngbDatepicker">

之后:

<input type="date" class="form-control" placeholder="dd/mm/yyyy"
formControlName="inputNascimento" ngbDatepicker #d="ngbDatepicker">

关于 react 形式的 Angular 6 日期格式 MM/dd/yyyy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com