Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@formatjs/intl-relativetimeformat

Package Overview
Dependencies
Maintainers
3
Versions
188
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@formatjs/intl-relativetimeformat

Formats JavaScript dates to relative time strings.

  • 11.4.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
3
Created

What is @formatjs/intl-relativetimeformat?

@formatjs/intl-relativetimeformat is a JavaScript library that provides a way to format relative time strings, such as '3 days ago' or 'in 2 weeks', using the ECMAScript Internationalization API (Intl.RelativeTimeFormat). It is part of the FormatJS suite of libraries designed to help with internationalization in JavaScript applications.

What are @formatjs/intl-relativetimeformat's main functionalities?

Basic Relative Time Formatting

This feature allows you to format relative time strings in a human-readable format. The example shows how to create an instance of IntlRelativeTimeFormat and use it to format relative times in English.

const { IntlRelativeTimeFormat } = require('@formatjs/intl-relativetimeformat');
const rtf = new IntlRelativeTimeFormat('en', { numeric: 'auto' });
console.log(rtf.format(-1, 'day')); // Output: 'yesterday'
console.log(rtf.format(2, 'week')); // Output: 'in 2 weeks'

Customizing Numeric Display

This feature allows you to customize how numeric values are displayed. By setting the 'numeric' option to 'always', you can ensure that the output always includes the numeric value, even for common time frames like 'yesterday' or 'tomorrow'.

const { IntlRelativeTimeFormat } = require('@formatjs/intl-relativetimeformat');
const rtf = new IntlRelativeTimeFormat('en', { numeric: 'always' });
console.log(rtf.format(-1, 'day')); // Output: '1 day ago'
console.log(rtf.format(2, 'week')); // Output: 'in 2 weeks'

Localization Support

This feature demonstrates the library's support for multiple languages. The example shows how to format relative time strings in French.

const { IntlRelativeTimeFormat } = require('@formatjs/intl-relativetimeformat');
const rtf = new IntlRelativeTimeFormat('fr', { numeric: 'auto' });
console.log(rtf.format(-1, 'day')); // Output: 'hier'
console.log(rtf.format(2, 'week')); // Output: 'dans 2 semaines'

Other packages similar to @formatjs/intl-relativetimeformat

Keywords

FAQs

Package last updated on 09 Dec 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc