在打字稿中将字符串转换为时间格式 [英] convert string to Time format in typescript
问题描述
我必须将我的服务器数据以字符串格式转换为 13:47
到 01:47PM
但我正在尝试使用 {{time |日期:"hh:MM"}}
和 {{task.time |date: 'shortTime'}}
但它显示了日期管道错误和参数错误
I have to convert my server data in string format as 13:47
to 01:47PM
but am trying with {{time | date:"hh:MM"}}
and {{task.time | date: 'shortTime'}}
but it showing an datepipe error and arugument error
运行时错误
InvalidPipeArgument: '13:17' 用于管道 'DatePipe'
InvalidPipeArgument: '13:17' for pipe 'DatePipe'
推荐答案
来自文档
https://angular.io/api/common/DatePipe
expression 是日期对象或数字(自 UTC 纪元以来的毫秒数)或 ISO 字符串(https://www.w3.org/TR/NOTE-datetime).
expression is a date object or a number (milliseconds since UTC epoch) or an ISO string (https://www.w3.org/TR/NOTE-datetime).
Angular 默认 DatePipe 仅支持日期对象或 ISOStrings().如果您想将任一格式转换为01:47PM"格式,则格式为hh:mmA"
Angular Default DatePipe only has support for Date Objects or ISOStrings(). If you want to convert either into format '01:47PM', the format would be 'hh:mmA'
这篇关于在打字稿中将字符串转换为时间格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!