我知道 json 不会以特殊方式处理日期,而是将它们作为服务器中的字符串提供。有没有办法提供具有 ISO 日期属性的 json 对象响应,并将其映射到已经将日期属性映射为 JS 日期的类?
例子:
杰森响应:
{
"data": {
"id": 1,
"name": "John",
"surname": "Smith",
"birthDate": "1986-05-04T22:59:59.000Z"
"subscriptionDate": "2020-06-28T14:36:43.498Z"
}
}
Run Code Online (Sandbox Code Playgroud)
我有这样的课程:
export class User {
id: string;
name: string;
surname: string;
birthDate: Date;
subscriptionDate: Date;
}
Run Code Online (Sandbox Code Playgroud)
我的服务方式:
getUser(id: string): Observable<User> {
return this.http.get<User>(`${this.UrlService}estabelecimentos/${id}`, { headers: this.authenticationService.jwt() }).catch(super.serviceError);
}
Run Code Online (Sandbox Code Playgroud)
但是当我在组件中使用它时,birthDate 和 subscriptionDate 始终被视为字符串。有没有办法将其转换为 JS 日期,而不必通过迭代 json 响应并修改对象来格式化每个请求中的日期?
I need to display dates based on locale. All goes well when I use the predefined formats (https://angular.io/api/common/DatePipe#pre-defined-format-options)
import { formatDate } from '@angular/common';
...
//if `format` is one from the predefined ones, for example 'shortDate', it works fine
return formatDate(value, format, this.localeService.currentLocale);
Run Code Online (Sandbox Code Playgroud)
But I need to display only months and years, format which doesn't exist in the predefined ones, therefor I need to create a custom format and give it as a parameter to the formatDate function. …
如何获取Angular用于显示日期的字符串形式的格式?我用的是date管道设置后,LOCALE_ID在我的app.module。
现在,我想检索“ dd / mm / yyyy”字符串,以放入我的输入占位符。
基本上,这里描述的只是Angular。
谢谢
[编辑]
为了使它更明确,我没有在寻找一种格式化日期的方法,这种方法已经完成了(使用本机date管道)。我想要代表此管道正在使用的格式的字符串。
因为我希望我的日期选择器占位符像
“日期(格式:XXXXXX)”
我希望“ XXXXXX”部分正确(例如,法语为“ jj / mm / aaaa”,英语为“ mm / dd / yyyy”)
我查询了如何在Angular 2中返回星期数。我没有找到这个问题的答案。我确实在https://docs.angularjs.org/api/ng/filter/date上发现在Angular 1中会是这样的:{{today | date:'w'}}但是这似乎在Angular 2中不起作用。我知道我可以编写一个函数来解决这个问题,但这似乎不切实际。我是否在有关Angular 2的文档中丢失了某些内容,或者尚未在其中实现?
在Angular 5.1中使用DatePipe,我需要以小写形式格式化句点字段类型(AM/PM).根据文件,
Tuesday, December 19, 7:00 am
Run Code Online (Sandbox Code Playgroud)
应该
date:'EEEE, MMMM d, h:mm a'
Run Code Online (Sandbox Code Playgroud)
但是,句点字段类型始终以大写形式显示,所以我看到:
Tuesday, December 19, 7:00 AM
Run Code Online (Sandbox Code Playgroud)
我做错了什么,或者这是Angular日期格式化的已知缺陷?
我正在使用Date()本地时区获取当前时间。我已将其格式化如下:
this.today = new Date();
from = new DatePipe('en-Us').transform(this.today, 'dd:MM:yyyy hh-mm-ss');
Run Code Online (Sandbox Code Playgroud)
现在我想将from日期时间转换Europe/London为时区。没有moment. 请帮我