Skip to content
the poor coder

angular show time ago

Poor Man
Published
Reading time
2 min read
On this page

How to Show Time Ago in Angular#

If you are working on an Angular project and want to display the time elapsed since a specific event, you can use the timeago-pipe extension. This will allow you to show the time ago in a human-readable format, such as "5 minutes ago" or "1 hour ago".

Installation#

To get started, you will first need to install the timeago-pipe extension using npm:

Bash
$ npm install time-ago-pipe --save

Usage#

Once you have installed the extension, you can use it in your Angular component by importing it and adding it to your declarations and imports arrays:

TypeScript
// Import the timeago pipe
import { TimeAgoPipe } from 'time-ago-pipe';

// Add the pipe to your declarations array
@NgModule({
  declarations: [
    AppComponent,
    TimeAgoPipe
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Once you have imported the pipe, you can use it in your HTML template by piping in a timestamp:

HTML
<p>Last updated: {{ lastUpdated | timeAgo }}</p>

The lastUpdated variable should be a timestamp, such as the result of Date.now().

Alternative Solution#

Another way to show time ago in Angular is to use the moment.js library. This library provides a lot of useful functions for working with dates and times, including a fromNow() function that will show the time elapsed since a specific event.

To use moment.js, you will first need to install it using npm:

Bash
$ npm install moment --save

After installing the library, you can import it into your Angular component and use it like this:

TypeScript
// Import moment.js
import * as moment from 'moment';

// Get the current time
const now = moment();

// Calculate the time elapsed since a specific event
const lastUpdated = moment('2021-01-01T00:00:00');
const elapsed = moment.duration(now.diff(lastUpdated));
const timeAgo = elapsed.humanize(true); // "a year ago"

// Output the time elapsed
console.log(timeAgo);

This example gets the current time using moment(), calculates the time elapsed since a specific event using the diff() function, and outputs the result using the humanize() function. You can customize the output by passing options to the humanize() function, such as { s: 'second', m: 'minute', h: 'hour', d: 'day', w: 'week', M: 'month', y: 'year' }.

Either of these solutions will allow you to show time ago in your Angular project, depending on your specific requirements and preferences.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC