无法将 java 日期对象转换为 angular 字符串格式
Unable to convert java date object to angular string format
我已将 java 日期对象以 JSON
格式保存在数据库中 12 月 31 日星期二 00:00:00 SGT 2019
我想将其转换为 Angular dd/MM/yyyy 格式。
我正在使用日期管道,但它给出了无效的日期管道参数。
请建议我可以用来转换 Angular 不在 Java 中的任何方法。
下面是我尝试在 Angular 中转换的代码。
Public pipe=new Datepipe (‘en-US’);
this.newVal= this.pipe.transform(this.newVal,”dd/MM/yyyy”);
如果来自后端的日期始终采用预期的格式,您可以硬编码转换格式
this.newVal = "tue dec 31 00:00:00 SGT 2019";
let temp = date.split(' ');
Public pipe=new Datepipe (‘en-US’);
this.newVal = pipe.transform(temp[5] +" " +temp[1]+" "+temp[2], "dd/MM/yyyy");
ngx-moment 中存在的 amParse 管道是一个很好的选择。
https://www.npmjs.com/package/ngx-moment#amparse-pipe
Parses a custom-formatted date into a moment object that can be used with the other pipes.
该管道的输出与日期非常相似,还可以与其他管道一起使用,将其输出为您想要的特定格式。
如果您只想在代码中而不是在 HTML 中使用它,您可以使用底层 moment.js 库中的一些函数:https://momentjs.com/docs/#/parsing/
我已将 java 日期对象以 JSON
格式保存在数据库中 12 月 31 日星期二 00:00:00 SGT 2019
我想将其转换为 Angular dd/MM/yyyy 格式。
我正在使用日期管道,但它给出了无效的日期管道参数。
请建议我可以用来转换 Angular 不在 Java 中的任何方法。
下面是我尝试在 Angular 中转换的代码。
Public pipe=new Datepipe (‘en-US’);
this.newVal= this.pipe.transform(this.newVal,”dd/MM/yyyy”);
如果来自后端的日期始终采用预期的格式,您可以硬编码转换格式
this.newVal = "tue dec 31 00:00:00 SGT 2019";
let temp = date.split(' ');
Public pipe=new Datepipe (‘en-US’);
this.newVal = pipe.transform(temp[5] +" " +temp[1]+" "+temp[2], "dd/MM/yyyy");
ngx-moment 中存在的 amParse 管道是一个很好的选择。 https://www.npmjs.com/package/ngx-moment#amparse-pipe
Parses a custom-formatted date into a moment object that can be used with the other pipes.
该管道的输出与日期非常相似,还可以与其他管道一起使用,将其输出为您想要的特定格式。 如果您只想在代码中而不是在 HTML 中使用它,您可以使用底层 moment.js 库中的一些函数:https://momentjs.com/docs/#/parsing/