Do you know how many days left till Christmas? Well this little handy library certainly does!
Among other things it allows adding dates, calculating intervals and, of course, formatting dates.
Install it with e.g. deno add npm:date-fns
and use like so:
import { formatDistanceToNow } from "npm:date-fns";
const xMas = new Date("2024-12-25");
console.log(`Christmas is ${formatDistanceToNow(xMas, { addSuffix: true })}`);
Run it with e.g. deno run -A main.ts
and you should get something like this:
deno run -A ./main.ts
Christmas is in 17 days
You can also do things like:
formatDistanceToNow(addDays(new Date(),3), { addSuffix: true }); // in 3 days
formatDistanceToNow(addDays(new Date(),-3), { addSuffix: true }); // 3 days ago
format(new Date(),'yyyy-M-dd'); // 2024-12-08
And a top of that you can also translate it to a localized string:
import { formatRelative, addHours, addDays } from "npm:date-fns";
import { sv, enUS } from "npm:date-fns/locale";
console.log(
formatRelative(addHours(new Date(), 1), new Date(), { locale: sv })
);
console.log(
formatRelative(addDays(new Date(), 1), new Date(), { locale: enUS })
);
Which would get you something like:
deno run -A ./main.ts
# idag kl. 21:09
# tomorrow at 8:09 PM
Liked the content and would love to have more of it all year long?
Top comments (0)