fancier date formatting

This commit is contained in:
Joseph Montanaro 2021-10-20 14:23:46 -07:00
parent d1d707bb6a
commit 23eb4341b6
2 changed files with 13 additions and 5 deletions

View File

@ -1,11 +1,11 @@
<script context="module">
import { formatDate } from './datefmt.js';
import Link from './Link.svelte';
export { Link as a };
</script>
<script>
export let title, date;
let pDate = new Date(date);
</script>
<svelte:head>
@ -14,6 +14,6 @@
<div id="post">
<h1>{title}</h1>
<p><em>Composed: {pDate.toLocaleDateString('en-US')}</em></p>
<p><em>{formatDate(date)}</em></p>
<slot></slot>
</div>

View File

@ -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}`;
}