【发布时间】:2018-07-20 08:03:33
【问题描述】:
我不太擅长处理日期时间。我将momentjs 用于ionic 应用程序 来操纵时间,但我想实现一些我做不到的事情。
我为此使用了一个管道,我想根据过去了多少天或几周、几个月或几年来显示。使用 relative time 将有助于我喜欢 fromNow() 方法和 momentjs 的 calendar()。但就我而言,我会有多个conditions。
这是我的管道下面的示例代码
transform(value: Date | moment.Moment, dateFormat: string): any {
if (moment(value) < moment(value).subtract(7, 'days')) {
return moment(value).format('llll') // Use this format if weeks, months or years has passed
} else if (moment(value) < moment(value).subtract(1, 'days')) {
return moment(value).calendar(); // Use calendar time if 1 day has passed
} else {
return moment(value).fromNow(); // Use relative time if within 24 hours
}
}
如果几秒、几分钟或几小时已经过去,直到 24 小时,我将使用 fromNow() 方法,但当几天过去时,我将使用 calendar(),如果几周、几个月或几年过去,则使用此 format('llll')。
有人可以帮我解释一下吗?
提前致谢。
【问题讨论】:
-
不清楚你想要达到什么。请详细说明
-
@VicJordan 编辑了它。对不起
标签: javascript datetime momentjs datetime-format relative-time-span