You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过angular2-moment将时间显示为1 mins ago?

Solution for Customizing amTimeAgo to Show "1 mins ago" in angular2-moment

To get the amTimeAgo pipe to display "1 mins ago" instead of the default "1 minute ago", you can customize Moment.js's relative time formatting directly—since angular2-moment relies on Moment.js under the hood. Here's how to do it step by step:

Step 1: Configure Moment.js Locale in Your App

In your Angular project (most commonly in app.module.ts), import Moment.js and update the English locale's relative time rules to match your desired output. This ensures the change applies globally across your app.

import { NgModule, OnInit } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { MomentModule } from 'angular2-moment'; // Make sure you've imported this module
import * as moment from 'moment';

import { AppComponent } from './app.component';

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, MomentModule],
  bootstrap: [AppComponent]
})
export class AppModule implements OnInit {
  ngOnInit(): void {
    // Override relative time strings for the English locale
    moment.updateLocale('en', {
      relativeTime: {
        m: '1 mins ago', // Customize singular minute text
        mm: '%d mins ago', // Customize plural minute text
        // Keep other formats default, or tweak them if needed
        s: 'a few seconds ago',
        h: '1 hour ago',
        hh: '%d hours ago',
        d: '1 day ago',
        dd: '%d days ago',
        M: '1 month ago',
        MM: '%d months ago',
        y: '1 year ago',
        yy: '%d years ago'
      }
    });
  }
}

Step 2: Verify Your Template Usage

Double-check your template code (note the date string should be wrapped in quotes to avoid syntax errors):

<div>{{'2018-04-16T05:56:45.200Z' | amTimeAgo}}</div>

Key Details

The amTimeAgo pipe uses Moment.js's fromNow() method behind the scenes. By updating the locale configuration, you're overriding Moment's default relative time strings:

  • m targets the singular case (1 minute)
  • mm targets the plural case (2+ minutes)

If you only need to modify minute-related text and leave other formats as default, you can simplify the configuration to just:

moment.updateLocale('en', {
  relativeTime: {
    m: '1 mins ago',
    mm: '%d mins ago'
  }
});

Moment will merge your customizations with its default locale settings automatically.

内容的提问来源于stack exchange,提问作者Aditya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:05:55