Skip to content

Commit

Permalink
js: fix date parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gorenburg committed Feb 3, 2025
1 parent ad61664 commit eb72fef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ export function getParsedDate(date: string): Date | undefined {
return undefined
}

export function formatDate(date: string, dateStyle: DateStyle = 'long', locales = 'en-US'): string {
export function formatDate(date: string | Date, dateStyle: DateStyle = 'long', locales = 'en-US'): string {
const dateValue = new Date(date)

if (dateValue) {
return new Intl.DateTimeFormat(locales, { dateStyle }).format(dateValue)
}

return date
return date.toString()
}

export function getFormatedDate(date: string, dateStyle: DateStyle = 'long') {
return formatDate(getParsedDate(date) || date, dateStyle)
}
4 changes: 2 additions & 2 deletions src/routes/[...slug]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import * as config from '$lib/config'
import { slugify } from '$lib/functions/slugify'
import { formatDate } from '$lib/utils'
import { getFormatedDate } from '$lib/utils'
import { onMount } from 'svelte'
import { base } from '$app/paths'
import { getPreviewImageUrl } from '$lib/functions/base_path'
Expand Down Expand Up @@ -54,7 +54,7 @@
<h1>{data.meta.title}</h1>
{/if}
<span class="post-meta">
<time class="post-date" datetime={formatDate(data.meta.date, 'full')}>{formatDate(data.meta.date)}</time>
<time class="post-date" datetime={getFormatedDate(data.meta.date, 'full')}>{getFormatedDate(data.meta.date)}</time>
<ul class="post-tags">
{#each data.meta.tags as tag}
Expand Down

0 comments on commit eb72fef

Please sign in to comment.