Here's a list of modern date time libraries for JavaScript and TypeScript:
Moment was a favorite library, and simply a requirement, for a large number of JavaScript projects that I've worked on. However it is no longer maintained. Luxon is the successor library for it.
The modern libraries mentioned above are actively maintained and all of them have smaller bundle sizes.
Bundle Sizes
Here's a comparison of bundle sizes:
library | minified | minified+gzipped |
---|---|---|
date-fns | 75.5kb | 17.2kb |
Luxon | 78.1kb | 23kb |
Day.js | 6.9kb | 3kb |
Moment | 294.9kb | 73.1kb |
API and Code Examples
Here's a comparison of some of their API functions and how the code looks.
date-fns
import { format, addDays, subBusinessDays, getOverlappingDaysInIntervals } from "date-fns";
format(new Date(2014, 1, 11), "MM/dd/yyyy");
const result = addDays(new Date(2014, 4, 1), 2);
subBusinessDays(new Date(2014, 8, 1), 10);
getOverlappingDaysInIntervals(
{ start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) },
{ start: new Date(2014, 0, 17), end: new Date(2014, 0, 21) }
);
Luxon
const now = DateTime.now();
DateTime.fromISO("2017-05-15T08:30:00")
dt = DateTime.now();
dt.plus({ hours: 3, minutes: 2 });
dt.minus({ days: 7 });
dt.startOf('day');
dt.endOf('hour');
var dur = Duration.fromObject({ hours: 2, minutes: 7 });
dt.plus(dur);
Day.js
var now = dayjs();
now.add(7, 'day')
dayjs().isBefore(dayjs('2011-01-01'))
Cover image by Marcin Nowak on Unsplash
Top comments (0)