diff --git a/src/components/PostList.svelte b/src/components/PostList.svelte index 77c0540..a79773b 100644 --- a/src/components/PostList.svelte +++ b/src/components/PostList.svelte @@ -7,13 +7,38 @@ let posts: {id: String, date: Date, title: String, author: String, description: String}[] = []; - let filteredPosts: {id: String, date: Date, title: String, author: String, description: String}[] = []; + let filteredPosts: {id: String, date: SimpleDate, title: String, author: String, description: String}[] = []; let error = false; let loading = true; let noPostMessage = ''; + class SimpleDate { + public year: number; + public month: number; + public day: number; + public datestring: string; + + constructor(dateString: string) { + const [year, month, day] = dateString.split('-').map(Number); + + this.year = year; + this.month = month; + this.day = day; + this.datestring = `${year}/${month}/${day}`; + } + + public toUnixTimestamp(): number { + // Create a Date object using the given year, month, and day. + // Month is zero-based, so subtract 1 from the month. + const date = new Date(Date.UTC(this.year, this.month - 1, this.day)); + + // Get the Unix timestamp by dividing the time (in milliseconds) by 1000. + return Math.floor(date.getTime() / 1000); +} + } + onMount(async () => { try { const response = await fetch('https://cdn.jonasjones.dev/blog/index.json'); @@ -26,15 +51,15 @@ return; } else { - // for each post, conver the date string to a Date object + // for each post, convert the date string to a Date object posts.forEach(post => { - post.date = new Date(post.date); + post.date = new SimpleDate(post.date); }); filteredPosts = posts.filter(post => post.title.toLowerCase().includes(query.toLowerCase())); // sort the posts by date - filteredPosts.sort((a, b) => b.date - a.date); + filteredPosts.sort((a, b) => b.date.toUnixTimestamp() - a.date.toUnixTimestamp()); loading = false; } } catch (error) { @@ -55,11 +80,11 @@