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

View File

@ -17,7 +17,15 @@ const weekdays = [
]; ];
export function formatDate(datestr) { export function formatDate(timestr) {
const date = new Date(datestr); const datestr = timestr.slice(0, 10);
const w = date.get 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}`;
} }