DEV Community

Cover image for Modern Date Time Libraries for JavaScript and TypeScript
Rudolf Olah
Rudolf Olah

Posted on • Updated on

Modern Date Time Libraries for JavaScript and TypeScript

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.

Update: Aside from datetime libraries, the module-replacements project on GitHub that lists JavaScripts modules that can be replaced and their alternatives

The modern libraries mentioned above are actively maintained and all of them have smaller bundle sizes.

timeline of javascript libraries for handling dates and times

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) }
);
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Day.js

var now = dayjs();
now.add(7, 'day')
dayjs().isBefore(dayjs('2011-01-01'))
Enter fullscreen mode Exit fullscreen mode

Cover image by Marcin Nowak on Unsplash

replacing moment.js infographic

Top comments (0)