将时间戳记转换为日期表达式,例如{{new Date(timestamp)}} [英] convert time stamp to Date inside angular expression like {{new Date(timestamp)}}
问题描述
今天我发现在角度表达式中转换日期不起作用.
Today I found that's it's not working to convert for a date inside angular expression.
所以我只是做类似
<th>{{new Date(elem.timestamp}}</th>
但是由于内部角度错误而失败.
But it fails with internal angular error.
那么为什么不能在角度表达式中强制转换为Date?
So why it's not possible to cast to Date inside angular expression?
推荐答案
因为角度表达式不接受任意Javascript代码,包括创建新对象.您正在寻找的就是所谓的角度滤波器,它们是为这种情况专门设计的.
Because angular expressions do not accept arbitrary Javascript code, including creating new objects. What you are looking for is called angular filters, they were specifically designed for this case.
所以不是
{{new Date(elem.timestamp)}}
您应该写
{{elem.timestamp | date: 'yyyy-MM-dd'}}
有关日期过滤器的更多信息,可以在此处找到.一般而言,可以在此处找到更多有关过滤器的信息.
More info on date filter can be found here. More on filters in general can be found here.
这篇关于将时间戳记转换为日期表达式,例如{{new Date(timestamp)}}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!