mirror of
https://github.com/jaronpate/folio.git
synced 2026-10-19 01:07:39 +00:00
fix date off by one
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
const YMD = /^(\d{4})-(\d{2})-(\d{2})/;
|
||||
const HAS_TZ = /(?:Z|[+-]\d{2}:?\d{2})$/i;
|
||||
|
||||
export function formatContentDate(value: string, month: 'short' | 'long' = 'short') {
|
||||
const match = YMD.exec(value);
|
||||
if (!match) return '';
|
||||
return new Date(Date.UTC(+match[1], +match[2] - 1, +match[3], 12)).toLocaleDateString(
|
||||
'en-US',
|
||||
{ month, day: 'numeric', year: 'numeric', timeZone: 'UTC' },
|
||||
);
|
||||
}
|
||||
|
||||
export function contentDateAttr(value: string) {
|
||||
if (HAS_TZ.test(value)) return new Date(value).toISOString();
|
||||
return YMD.exec(value)?.[0] ?? '';
|
||||
}
|
||||
@@ -5,16 +5,14 @@ export function projectEntryHref(page: {
|
||||
return page.href || `/projects${page.path}`;
|
||||
}
|
||||
|
||||
export function sortProjects<
|
||||
T extends { title: string; date?: Date | string | null },
|
||||
>(pages: T[]) {
|
||||
export function sortProjects<T extends { title: string; date?: string | null }>(
|
||||
pages: T[],
|
||||
) {
|
||||
return [...pages].sort((a, b) => {
|
||||
const aTime = a.date ? new Date(a.date).getTime() : Number.NaN;
|
||||
const bTime = b.date ? new Date(b.date).getTime() : Number.NaN;
|
||||
const aHasDate = !Number.isNaN(aTime);
|
||||
const bHasDate = !Number.isNaN(bTime);
|
||||
if (aHasDate && bHasDate && aTime !== bTime) return bTime - aTime;
|
||||
if (aHasDate !== bHasDate) return aHasDate ? -1 : 1;
|
||||
if (!a.date && !b.date) return a.title.localeCompare(b.title);
|
||||
if (!a.date) return 1;
|
||||
if (!b.date) return -1;
|
||||
if (a.date !== b.date) return b.date.localeCompare(a.date);
|
||||
return a.title.localeCompare(b.title);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user