Logo

dev-resources.site

for different kinds of informations.

Day 8: D-8 🎱

Published at
12/8/2024
Categories
webdev
beginners
deno
webadvent
Author
valeriavg
Categories
4 categories in total
webdev
open
beginners
open
deno
open
webadvent
open
Author
9 person written this
valeriavg
open
Day 8: D-8 🎱

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

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

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

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

Which would get you something like:

deno run -A ./main.ts
# idag kl. 21:09
# tomorrow at 8:09 PM
Enter fullscreen mode Exit fullscreen mode

Liked the content and would love to have more of it all year long?

Buy Me A Coffee

deno Article's
30 articles in total
Favicon
Deno docker
Favicon
Day 24: Paint by Pixels 🎨
Favicon
Day 22: How the Tables have turned πŸ“
Favicon
Day 20: Not a Dedent! 🚧
Favicon
Day 23: Terminal Images πŸ–ΌοΈ
Favicon
Day 19: Highlight'em up! πŸ”–
Favicon
Day 18: Got a millisecond? ⏱️
Favicon
You can omit `run` from `deno run` command
Favicon
Day 17: Terminal Links & Other Escapes πŸ”—
Favicon
Day 8: D-8 🎱
Favicon
Day 7: Your input is valid πŸ–οΈ
Favicon
πŸš€ Rust Coders, Don’t Miss These 25 Resource Picks
Favicon
Day 9: Terminal Forms πŸ“‡
Favicon
Dynamic DNS sync with Cloudflare
Favicon
Day 14: Keep on Spinning! ♻️
Favicon
πŸš€ Automate Your PostgreSQL Backups with Ease! 🐳
Favicon
Day 21: In the name of Progress! πŸ“ˆ
Favicon
πŸš€ Automate Your PostgreSQL Backups with Ease! 🐳
Favicon
Runtime challenge: Bun vs Node
Favicon
I built a Fullstack Deno 2 application
Favicon
Self Writing Lang Graph State
Favicon
Supabase Just Got More Powerful: Queue, Cron, and Background Tasks in Edge Functions
Favicon
Day 4: ASCII Art Fonts πŸ–‹οΈ
Favicon
Day 13: I love Boxes! πŸ“¦
Favicon
#2 Daily Rabbit Holes: Diving Deeper into Rust, V8, and the JavaScriptℒ️ Saga
Favicon
Day 12: Pico-co-colors πŸ₯
Favicon
πŸ†š Bun vs Deno: When to Use Each in Your Projects?
Favicon
Exploring Bun.js Over Deno: A Humble Developer's Tale
Favicon
Day 10: Ho-ho-hono! πŸ”₯
Favicon
Benchmarking in Node.js vs Deno: A Comprehensive Comparison

Featured ones: