Home > Web Front-end > JS Tutorial > Introducing date-formatter-i: Simplify i for Dates in JavaScript

Introducing date-formatter-i: Simplify i for Dates in JavaScript

Susan Sarandon
Release: 2025-01-10 20:30:43
Original
735 people have browsed it

Introducing date-formatter-i: Simplify i for Dates in JavaScript

As developers, we often deal with dates in our applications, and it’s never as simple as it seems. What format should we use? How do we account for user locales? What about relative time like "3 days ago"?

Introducing date-formatter-i18n, a lightweight NPM package that makes date formatting and localization effortless.

Here’s what it offers:


Features

  • ? Date Formatting: Convert raw dates into readable formats across locales.

  • ⏳ Relative Time: Display times like "2 days ago" or "in 1 hour" dynamically.

  • ? i18n Support: Easily switch between languages like English, French, and German.


Why Use This Library?

Existing solutions can be heavy, complex, or lack proper i18n support. date-formatter-localized focuses on simplicity, making it perfect for multilingual apps.

Here’s an example:

import DateFormatter from 'date-formatter-i18n';

// Create an instance of DateFormatter
const dateFormatter = new DateFormatter('en');

const formattedDate = dateFormatter.format('2023-12-25'); 
// Output: "December 25, 2023"

// Relative time
const pastDate = new Date(Date.now() - 3 * 24 * 60 * 60 * 1000);
console.log(dateFormatter.formatRelative(pastDate));
// Output: 3 days ago

Copy after login

Get Started

To install:

npm install date-formatter-localized


Contribute

Want to add more locales or features? Head over to the GitHub repo and contribute!

If you find it helpful, please star ⭐ the repository to show your support!

Let’s make date formatting smarter, simpler, and more global. ?

The above is the detailed content of Introducing date-formatter-i: Simplify i for Dates in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template