1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-21 06:21:17 -04:00

use <time> element for all dates

This commit is contained in:
2022-04-16 15:59:28 -04:00
parent 1648464e0e
commit fa968dbcd2
11 changed files with 217 additions and 202 deletions

View File

@@ -3,13 +3,20 @@ import { formatInTimeZone } from "date-fns-tz";
import enUS from "date-fns/locale/en-US";
import { timeZone } from "../config";
export type FlexibleDate = string | number | Date;
// normalize timezone across the site, both server and client side, to prevent hydration errors.
// format defaults to "Apr 4, 2022, 3:04 PM EDT", see https://date-fns.org/v2.28.0/docs/format
export const formatDateTZ = (date: string | number | Date, formatStr = "PPp zzz", options = {}) => {
export const formatDateTZ = (date: FlexibleDate, formatStr = "PPp zzz", options = {}) => {
return formatInTimeZone(new Date(date), timeZone, formatStr, { locale: enUS, ...options });
};
// returns a timezone-less, machine-readable string.
export const formatDateISO = (date: FlexibleDate) => {
return new Date(date).toISOString();
};
// returns "5 minutes ago", "1 year ago", etc.
export const formatTimeAgo = (date: string | number | Date, options = {}) => {
export const formatTimeAgo = (date: FlexibleDate, options = {}) => {
return formatDistanceToNowStrict(new Date(date), { addSuffix: true, locale: enUS, ...options });
};