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 918405a commit 38b1d2a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/lib/requests/posts.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { itemsPerPage } from '$lib/config'
import type { GetPostsParams, GetPostsResponse, Post } from '$lib/types'
import { getParsedDate } from '$lib/utils'

const slugRegExp = new RegExp(/^[0-9]{4}-[0-9]{2}-[0-9]{2}-/)

Expand All @@ -21,7 +22,8 @@ export async function getPosts(params?: GetPostsParams): Promise<GetPostsRespons
if (file && typeof file === 'object' && 'metadata' in file && slug) {
const metadata = file.metadata as Omit<Post, 'slug'>
const post = { ...metadata, slug } satisfies Post
post.date = new Date(post.date).toUTCString()
const parsedDate = getParsedDate(post.date)
post.date = parsedDate ? parsedDate.toUTCString() : post.date
posts.push(post)
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ type DateStyle = Intl.DateTimeFormatOptions['dateStyle']

const timePattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})\s(?<hour>\d{2}):(?<minute>\d{2}):(?<second>\d{2})\s(?<timezone>[+-]\d{4})/;

export function formatDate(date: string, dateStyle: DateStyle = 'long', locales = 'en-US'): string {
export function getParsedDate(date: string): Date | undefined {
const match = date.match(timePattern)

if (match) {
Expand All @@ -16,10 +16,18 @@ export function formatDate(date: string, dateStyle: DateStyle = 'long', locales
second: groups?.second,
timezone: groups?.timezone
}
const dateToFormat = new Date(`${components.year}-${components.month}-${components.day}T${components.hour}:${components.minute}:${components.second}`)
return new Intl.DateTimeFormat(locales, { dateStyle }).format(dateToFormat)
return new Date(`${components.year}-${components.month}-${components.day}T${components.hour}:${components.minute}:${components.second}Z`)
}

return undefined
}

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

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

const dateToFormat = new Date(date)
return Intl.DateTimeFormat(locales, { dateStyle }).format(dateToFormat)
return date
}
2 changes: 1 addition & 1 deletion src/routes/tag/[slug]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{#each data.posts.items as post}
<li class="tag-list-item">
<h2><a href="{base}/{post.slug}" class="title">{post.title}</a></h2>
<p class="date">{formatDate(post.date)}</p>
<time class="date" datetime={formatDate(post.date)}>{formatDate(post.date)}</time>
</li>
{/each}
</ul>
Expand Down

0 comments on commit 38b1d2a

Please sign in to comment.