All Projects → ihym → ngx-timeago

ihym / ngx-timeago

Licence: MIT license
⏰ Live updating timestamps in Angular 6+

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to ngx-timeago

Ng2 Search Filter
Angular 2 / Angular 4 / Angular 5 custom pipe npm module to make a search filter on any input, 🔥 100K+ downloads
Stars: ✭ 137 (+95.71%)
Mutual labels:  pipe
Lwrb
Lightweight generic ring buffer manager library
Stars: ✭ 215 (+207.14%)
Mutual labels:  pipe
pipe
Functional Pipeline in Go
Stars: ✭ 30 (-57.14%)
Mutual labels:  pipe
Onhold
🔊 Play sounds while and after shell jobs complete
Stars: ✭ 146 (+108.57%)
Mutual labels:  pipe
Ppipe
pipes values through functions, an alternative to using the proposed pipe operator ( |> ) for ES
Stars: ✭ 192 (+174.29%)
Mutual labels:  pipe
Termsql
Convert text from a file or from stdin into SQL table and query it instantly. Uses sqlite as backend. The idea is to make SQL into a tool on the command line or in scripts.
Stars: ✭ 230 (+228.57%)
Mutual labels:  pipe
Lessmd
A small markdown viewer/converter for unix terminal.
Stars: ✭ 122 (+74.29%)
Mutual labels:  pipe
pipeffmpeg
A frontend for ffmpeg using only pipes, not under GPL, but under BSD license.
Stars: ✭ 56 (-20%)
Mutual labels:  pipe
Lightweightrunloop A Reactor Style Nsrunloop
NSRunLoop Reactor Style Implementation: Using BSD kqueue implements iOS/Mac NSRunLoop and RunLoop-Relative Foundation such as perform selector(or delay some times) on other thread , Timer, URLConnection ,LWStream(LWInputStream、LWOutputStream) , LWPort(LWSocketPort) etc.
Stars: ✭ 196 (+180%)
Mutual labels:  pipe
remark-directive
remark plugin to support directives
Stars: ✭ 137 (+95.71%)
Mutual labels:  directive
Fluids
Fluid dynamics component of Chemical Engineering Design Library (ChEDL)
Stars: ✭ 154 (+120%)
Mutual labels:  pipe
Pipeline.rs
☔️ => ⛅️ => ☀️
Stars: ✭ 188 (+168.57%)
Mutual labels:  pipe
rtss
Relative TimeStamps for Stuff
Stars: ✭ 42 (-40%)
Mutual labels:  timestamp
Pipetools
Functional plumbing for Python
Stars: ✭ 143 (+104.29%)
Mutual labels:  pipe
angular-datetime-inputs
📅 Angular directives for datetime inputs
Stars: ✭ 20 (-71.43%)
Mutual labels:  directive
Ngx Filter Pipe
𝗩 Angular 5+ pipeline for array filtering.
Stars: ✭ 129 (+84.29%)
Mutual labels:  pipe
Ngx Order Pipe
▼ Angular 5+ orderBy pipe
Stars: ✭ 224 (+220%)
Mutual labels:  pipe
time decode
A timestamp and date decoder written for python 3
Stars: ✭ 24 (-65.71%)
Mutual labels:  timestamp
ngx-access
Add access control to your components using hierarchical configuration with logical expressions.
Stars: ✭ 21 (-70%)
Mutual labels:  directive
angular-inviewport
A simple lightweight library for Angular with no other dependencies that detects when an element is within the browser viewport and adds a "sn-viewport-in" or "sn-viewport-out" class to the element
Stars: ✭ 72 (+2.86%)
Mutual labels:  directive

ngx-timeago Build Status npm version npm License: MIT

Live updating timestamps in Angular 6+.

https://ihym.github.io/ngx-timeago/

Get the complete changelog here: https://github.com/ihym/ngx-timeago/releases

Installation

First you need to install the npm module:

npm install ngx-timeago --save

Choose the version corresponding to your Angular version:

Angular ngx-timeago
10 2.x+
6,7,8,9 1.x+

Usage

1. Import the TimeagoModule:

Once installed you need to import the main module into your application module by calling TimeagoModule.forRoot().

Make sure you only call this method in the root module of your application, most of the time called AppModule. This method allows you to configure the TimeagoModule by specifying a formatter, clock and/or an intl service. You should end up with code similar to this:

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { TimeagoModule } from 'ngx-timeago';

@NgModule({
  imports: [
    BrowserModule,
    TimeagoModule.forRoot()
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }
SharedModule

If you use a SharedModule that you import in multiple other feature modules, you can export the TimeagoModule to make sure you don't have to import it in every module.

@NgModule({
  exports: [
    CommonModule,
    TimeagoModule
  ]
})
export class SharedModule { }
Lazy loaded modules

When you lazy load a module, you should use the forChild static method to import the TimeagoModule.

Since lazy loaded modules use a different injector from the rest of your application, you can configure them separately with a different formatter/clock/intl service.

@NgModule({
  imports: [
    TimeagoModule.forChild({
      formatter: {provide: TimeagoFormatter, useClass: CustomFormatter},
      clock: {provide: TimeagoClock, useClass: CustomClock},
      intl: {provide: TimeagoIntl, useClass: CustomIntl},
    })
  ]
})
export class LazyLoadedModule { }
I18n

By default, there is no intl service available, as the default formatter doesn't provide language support. You should provide one, if you end up with a formatter that needs it (either TimeagoCustomFormatter which is provided by the lib or your own). The purpose of the intl service is to contain all the necessary i18n strings used by your formatter.

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { Timeago, TimeagoIntl, TimeagoFormatter, TimeagoCustomFormatter } from 'ngx-timeago';
import { AppComponent } from './app';

export class MyIntl extends TimeagoIntl {
// do extra stuff here...
}

@NgModule({
  imports: [
    BrowserModule,
    TimeagoModule.forRoot({
      intl: { provide: TimeagoIntl, useClass: MyIntl },
      formatter: { provide: TimeagoFormatter, useClass: TimeagoCustomFormatter },
    })
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

There is support for a large number of languages out of the box. This support is based on the string objects taken from jquery-timeago.

To use any of the languages provided, you will have to import the language strings and feed them to the intl service.

import { Component } from '@angular/core';
import { TimeagoIntl } from 'ngx-timeago';
import {strings as englishStrings} from 'ngx-timeago/language-strings/en';

@Component({
  selector: 'app',
  template: `
    <div timeago [date]="1553683912689"></div>
  `
})
export class AppComponent {
  constructor(intl: TimeagoIntl) {
    intl.strings = englishStrings;
    intl.changes.next();
  }
}

You can also customize the language strings or provide your own.

2. Use the pipe or the directive:

This is how you do it with the pipe:

<div>{{1553683912689 | timeago:live}}</div>

And in your component define live (true by default).

This is how you use the directive:

<div timeago [date]="1553683912689" [live]="live"></div>

API

Write your own formatter

If you want to write your own formatter, you need to create a class that implements TimeagoFormatter. The only required method is format that must return the final string.

Example

Once you've defined your formatter, you can provide it in your configuration.

@NgModule({
  imports: [
    BrowserModule,
    TimeagoModule.forRoot({
      formatter: {provide: TimeagoFormatter, useClass: CustomFormatter}
    })
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Write your own clock

The only required method to build your own clock, is tick that must return an Observable<any>. Whenever this observable emits, the timestamp will be updated, using your formatter (and intl, if available).

import { TimeagoClock } from 'ngx-timeago';
import { Observable, interval } from 'rxjs';

// ticks every 2s
export class MyClock extends TimeagoClock {
  tick(then: number): Observable<number> {
    return interval(2000);
  }
}

Setup the clock in your module import by adding it to the forRoot (or forChild) configuration.

@NgModule({
  imports: [
    BrowserModule,
    TimeagoModule.forRoot({
      clock: {provide: TimeagoClock, useClass: MyClock},
    })
  ],
  providers: [
  ],
  bootstrap: [AppComponent]
})
export class AppModule { }

Contribute

ngx-timeago is packaged with ng-packagr and then imported into an Angular CLI app. To run the demo, do the following steps:

$ npm install
$ npm run build:lib
$ npm start dev

MIT © Vasilis Diakomanolis

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].