Need to subtract dates in Node.js? No problem, let’s use Java for that.

After installing GraalVM using SDKMAN:

$ sdk install java 20.0.1-graal

as well as installing GraalVM’s Node.js runtime using gu1:

$ gu install nodejs

we can now perform some black magic:

// index.js

// "Import" LocalDate & ChronoUnit from ☕️
const LocalDate = Java.type("java.time.LocalDate");
const ChronoUnit = Java.type("java.time.temporal.ChronoUnit");

const firstDate = LocalDate.of(2023, 7, 20);
const secondDate = LocalDate.of(2023, 8, 4);

console.log(ChronoUnit.DAYS.between(firstDate, secondDate));
$ ~/.sdkman/candidates/java/20.0.1-graal/languages/nodejs/bin/node --polyglot index.js
15

Look ma, no Moment.js, Day.js or date-fns! (Just a kickass runtime.)



  1. gu, or GraalVM Updater, comes pre-packaged with GraalVM - you don’t need to install it independently. ↩︎