Aurelia 中的时间减法

Time subtraction in Aurelia

我想打印发生在 'startDateTime' 和 'endDateTime' 之间的事件的持续时间,以分钟或秒表示(如果少于 1 分钟)。

也就是说,${startDateTime | dateFormat:"YYYY-MM-DD HH:mm"}是2018-09-0711:57,${startDateTime | dateFormat:"YYYY-MM-DD HH:mm"}是2018-09-0713:00。 我要打印的是 63 minutes.

在 PHP 中,我会 ->getTimestamp(),但在 Aurelia 中,我什至不知道该尝试什么。

我确实用 ${endDateTime| dateFormat:"HH:mm:ss" - startDateTime| dateFormat:"HH:mm:ss"} 之类的东西进行了测试,但这行不通,因为它没有将整个日期时间转换为秒或分钟...

因此,在我看来是否有一个干净的解决方案可以实施?

你想要的是相对时间,它正在发送给浏览器,但现在,你将不得不为它使用 polyfill / library。您可以找到来自雅虎的一个:https://github.com/yahoo/intl-relativeformat

我用值转换器解决了它。

import moment = require("moment");

export class DurationValueConverter {
    public toView(startAt, endAt) {
        if (!endAt) {
            // If end date is missing, use the current date and time.
            endAt = moment();
        }

        const duration = moment.duration(moment(endAt).diff(moment(startAt)));

        return duration.humanize();
    }
}

用法:${startedAt | duration:endedAt}