
date-fns Has a Free API — Here's How to Handle Dates Without Moment.js
date-fns is a modern JavaScript date utility library with 200+ tree-shakeable functions for parsing, formatting, and manipulating dates. Installation npm install date-fns Formatting import { format , formatDistance , formatRelative } from " date-fns " ; const now = new Date (); console . log ( format ( now , " yyyy-MM-dd HH:mm:ss " )); // 2026-03-29 14:30:00 console . log ( format ( now , " EEEE, MMMM do yyyy " )); // Saturday, March 29th 2026 console . log ( formatDistance ( new Date ( 2026 , 2 , 25 ), now , { addSuffix : true })); // 4 days ago Parsing import { parse , parseISO , isValid } from " date-fns " ; const date1 = parseISO ( " 2026-03-29T10:00:00Z " ); const date2 = parse ( " 29/03/2026 " , " dd/MM/yyyy " , new Date ()); console . log ( isValid ( date1 )); // true Comparison import { isBefore , isAfter , differenceInDays , isWithinInterval } from " date-fns " ; const start = new Date ( 2026 , 0 , 1 ); const end = new Date ( 2026 , 11 , 31 ); console . log ( differenceInDays
Continue reading on Dev.to Webdev
Opens in a new tab

