Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@formatjs/intl-relativetimeformat
Advanced tools
Formats JavaScript dates to relative time strings.
@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.
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'
date-fns is a modern JavaScript date utility library that provides a variety of functions for manipulating and formatting dates. It includes a function called formatDistance, which can be used to format relative time strings. Compared to @formatjs/intl-relativetimeformat, date-fns offers a broader range of date manipulation utilities but may require more configuration for internationalization.
Moment.js is a widely-used JavaScript library for parsing, validating, manipulating, and formatting dates. It includes a fromNow method that can be used to format relative time strings. While Moment.js is feature-rich and easy to use, it is larger in size compared to @formatjs/intl-relativetimeformat and has been deprecated in favor of more modern alternatives.
Luxon is a modern JavaScript library for working with dates and times, created by one of the Moment.js developers. It provides a method called toRelative, which can be used to format relative time strings. Luxon is a more modern and lightweight alternative to Moment.js, with better support for internationalization and time zones.
We've migrated the docs to https://formatjs.github.io/docs/polyfills/intl-relativetimeformat.
FAQs
Formats JavaScript dates to relative time strings.
The npm package @formatjs/intl-relativetimeformat receives a total of 207,147 weekly downloads. As such, @formatjs/intl-relativetimeformat popularity was classified as popular.
We found that @formatjs/intl-relativetimeformat demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.