ECMAScript Modules for Working with Dates in JavaScript (Using date-fns)
date-fns and Module Systems
- date-fns offers its functionalities in two flavors:
- CommonJS
This is the traditional module system for Node.js. It's located in the root of the package you install. - ECMAScript Modules (ESM)
This is the newer module system supported by modern browsers and bundlers like Webpack. It's located in theesm
subdirectory within the date-fns package.
- CommonJS
Using ESM with date-fns
- To import specific functions from date-fns using ESM, you'll use the
import
syntax:
import { format, isToday } from "date-fns/esm/index.js";
const today = new Date();
const formattedDate = format(today, "yyyy-MM-dd");
console.log(formattedDate); // Output: yyyy-mm-dd (formatted date)
if (isToday(today)) {
console.log("It's today!");
}
Benefits of ESM
- ESM offers several advantages over CommonJS:
- Better for Browsers
ESM is the native module system for browsers, leading to cleaner integration. - Tree-shaking
Modern bundlers can leverage ESM to remove unused code from your final bundle, reducing file size.
- Better for Browsers
General Considerations
- While ESM is the future for browser development, some legacy projects might still use CommonJS.
- Ensure your bundler or environment supports ESM.
- date-fns ESM documentation: [date fns esm ON date-fns.org]
Formatting a Date
import { format } from "date-fns/esm/format";
const someDate = new Date(2024, 5, 17); // June 17, 2024
const formattedDate = format(someDate, "PPp, EEEE"); // Outputs: "Mon, June 17, 2024, Friday"
console.log(formattedDate);
Comparing Dates
import { isAfter, isBefore } from "date-fns/esm";
const today = new Date();
const futureDate = new Date(2025, 0, 1); // January 1, 2025
if (isAfter(futureDate, today)) {
console.log("The future date is after today.");
} else if (isBefore(futureDate, today)) {
console.log("The future date is before today (shouldn't happen here).");
}
import { differenceInDays, differenceInHours } from "date-fns/esm";
const eventDate = new Date(2024, 6, 20); // June 20, 2024
const daysLeft = differenceInDays(eventDate, new Date());
if (daysLeft > 0) {
console.log(`${daysLeft} days left until the event.`);
} else {
const hoursLeft = differenceInHours(eventDate, new Date());
console.log(`${hoursLeft} hours left until the event (or it might have already passed).`);
}
CommonJS Modules
- Use Case
Primarily for legacy Node.js projects that haven't transitioned to ESM yet. - This is the traditional module system for Node.js. It uses
require
andmodule.exports
for importing and exporting modules.
AMD (Asynchronous Module Definition)
- Use Case
Less common these days, but you might encounter it in older browser-based codebases. - An older module system focused on asynchronous loading, often used with libraries like RequireJS.
UMD (Universal Module Definition)
- Use Case
Useful for creating libraries that need to function in various module systems. - A module format that can work in both CommonJS and AMD environments, providing compatibility across different systems.
Choosing the Right Option
- Creating Reusable Libraries
Consider UMD for wider compatibility. - Legacy Node.js Projects
Stick with CommonJS unless you're actively migrating to a newer system. - Modern Browsers and Bundlers
Use ESM for better browser integration and potential tree-shaking benefits.
- Community Support
ESM is actively supported and has a growing ecosystem. While CommonJS has a vast existing library base, new libraries are increasingly adopting ESM. - Bundlers like Webpack and Rollup
These tools can handle different module systems and often provide configuration options for specifying your preferred format.