summaryrefslogtreecommitdiff
path: root/src/utils/date.ts
blob: b8c03763dbede3f5a6ed0dd1b807944ff7931ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import type { CollectionEntry } from "astro:content";
import { siteConfig } from "@/site.config";

export function getFormattedDate(
	date: Date | undefined,
	options?: Intl.DateTimeFormatOptions,
): string {
	if (date === undefined) {
		return "Invalid Date";
	}

	return new Intl.DateTimeFormat(siteConfig.date.locale, {
		...(siteConfig.date.options as Intl.DateTimeFormatOptions),
		...options,
	}).format(date);
}

export function collectionDateSort(
	a: CollectionEntry<"post" | "micro">,
	b: CollectionEntry<"post" | "micro">,
) {
	return b.data.publishDate.getTime() - a.data.publishDate.getTime();
}