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:
? 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.
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
To install:
npm install date-formatter-localized
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!