From 23eb4341b62fcf5f4504e372d954e2fe6e014c21 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Wed, 20 Oct 2021 14:23:46 -0700 Subject: [PATCH] fancier date formatting --- src/lib/Post.svelte | 4 ++-- src/lib/datefmt.js | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/lib/Post.svelte b/src/lib/Post.svelte index c679330..57166c9 100644 --- a/src/lib/Post.svelte +++ b/src/lib/Post.svelte @@ -1,11 +1,11 @@ @@ -14,6 +14,6 @@

{title}

-

Composed: {pDate.toLocaleDateString('en-US')}

+

{formatDate(date)}

diff --git a/src/lib/datefmt.js b/src/lib/datefmt.js index 7ae04df..e09f435 100644 --- a/src/lib/datefmt.js +++ b/src/lib/datefmt.js @@ -17,7 +17,15 @@ const weekdays = [ ]; -export function formatDate(datestr) { - const date = new Date(datestr); - const w = date.get +export function formatDate(timestr) { + const datestr = timestr.slice(0, 10); + const [year, month, monthday] = datestr.split('-').map(n => parseInt(n)); + // for some reason the Date constructor expects the month index instead of ordinal + const weekdayIdx = new Date(year, month - 1, monthday).getDay(); + const names = { + month: months[month - 1], + monthday: ordinals[monthday - 1], + weekday: weekdays[weekdayIdx], + } + return `${names.weekday}, the ${names.monthday} of ${names.month}, A.D. ${year}`; }