@tuhinkarmakar/relative-time
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

ng-relative-time npm version

A simple relative time formatter for Angular. Uses bleeding-edge formatting APIs for headache-free internationalization.

Table of Contents

Features

  • Internationalization: Formats strings using the browser's locale. Uses built-in browser APIs for internationalization. No setup required. The heavy-lifting is done by the browser.
  • Time Travelling Formatter: The formatter works with both past and future timestamps.

Installation

Run npm i @tuhinkarmakar/relative-time to install library into your project.

Usage

  1. First import the RelativeTimeModule in your root module.
import { BrowserModule } from "@angular/platform-browser"
import { NgModule } from "@angular/core"
import { RelativeTimeModule } from "@tuhinkarmakar/relative-time"   // <-- Import the module like this

import { AppComponent } from "./app.component"

@NgModule({
    declarations: [AppComponent],
    imports:      [BrowserModule, RelativeTimeModule],    // <-- Add it to the imports array
    providers:    [],
    bootstrap:    [AppComponent],
})
export class AppModule {}
  1. Use the relativeTime pipe with a datetime string accepted by the Date() constructor. See Date.parse() MDN page for supported formats.
{{ timestamp | relativeTime }} // Output: yesterday / tomorrow / 5 seconds ago / in 5 months etc.

Browser Support

Since the library uses Intl.RelativeTimeFormat, it only works on Google Chrome 71+ and Mozilla Firefox 65+. Browser support will get better as more vendors start implmenting the proposal.

See this MDN page for details.

Task List

  • [ ] Add supoort for Schemantics
  • [ ] Add a declaration file for Intl.RelativeTimeFormat

Contributing

Report bugs and share enhancement ideas here. PRs are also welcome.

Package Sidebar

Install

npm i @tuhinkarmakar/relative-time

Weekly Downloads

0

Version

1.0.1

License

MIT

Unpacked Size

79.3 kB

Total Files

23

Last publish

Collaborators

  • tuhinkarmakar