前段时间在 angular 时刻 js

Time ago in angular moment js

我正在使用 Moment js and Angular Moment js to calculate time ago for my website. Everything is working perfectly except one thing. I don't know whether is it possible or not. I checked their humanize() 文档,但没有找到任何有用的信息。

<span am-time-ago="messagedata.created | amFromUnix"></span>

结果:a day ago 但我想要 1 day ago。可能吗?如果是怎么办?

这些消息取决于 moment.js 使用的语言环境,但您可以更新语言环境以提供不同版本的消息

moment.updateLocale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "seconds",
        m:  "a minute",
        mm: "%d minutes",
        h:  "an hour",
        hh: "%d hours",
        d:  "a day",
        dd: "%d days",
        M:  "a month",
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

使用 angular 指令,您应该能够在应用程序启动时配置 moment 实例

var myapp = angular.module('myapp', ['angularMoment']);
myapp.run(function(amMoment) {
    amMoment.changeLocale(...);
});

您可以查看moment.js documentation about customisation了解更多详情

从那一刻开始检查 fromnow 部分,因为您要显示与今天相关的日期。