mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-21 15:01:18 -04:00
fix "time ago" calculation
This commit is contained in:
@@ -7,8 +7,6 @@ import dayjsAdvancedFormat from "dayjs/plugin/advancedFormat";
|
|||||||
import "dayjs/locale/en";
|
import "dayjs/locale/en";
|
||||||
import { timeZone } from "../config";
|
import { timeZone } from "../config";
|
||||||
|
|
||||||
// normalize timezone and locale across the site, both server and client side, to prevent hydration errors by returning
|
|
||||||
// an instance of dayjs with these defaults set.
|
|
||||||
const IsomorphicDayJs = (date?: dayjs.ConfigType): dayjs.Dayjs => {
|
const IsomorphicDayJs = (date?: dayjs.ConfigType): dayjs.Dayjs => {
|
||||||
// plugins
|
// plugins
|
||||||
dayjs.extend(dayjsUtc);
|
dayjs.extend(dayjsUtc);
|
||||||
@@ -17,13 +15,14 @@ const IsomorphicDayJs = (date?: dayjs.ConfigType): dayjs.Dayjs => {
|
|||||||
dayjs.extend(dayjsLocalizedFormat);
|
dayjs.extend(dayjsLocalizedFormat);
|
||||||
dayjs.extend(dayjsAdvancedFormat);
|
dayjs.extend(dayjsAdvancedFormat);
|
||||||
|
|
||||||
return dayjs.tz(date, timeZone).locale("en");
|
return dayjs(date).locale("en");
|
||||||
};
|
};
|
||||||
|
|
||||||
// simple wrapper around dayjs.format()
|
// simple wrapper around dayjs.format() to normalize timezone across the site, both server and client side, to prevent
|
||||||
|
// hydration errors by returning an instance of dayjs with these defaults set.
|
||||||
// date defaults to now, format defaults to ISO 8601 (e.g. 2022-04-07T21:53:33-04:00)
|
// date defaults to now, format defaults to ISO 8601 (e.g. 2022-04-07T21:53:33-04:00)
|
||||||
export const formatDate = (date?: dayjs.ConfigType, formatStr?: string) => {
|
export const formatDate = (date?: dayjs.ConfigType, formatStr?: string) => {
|
||||||
return IsomorphicDayJs(date).format(formatStr);
|
return IsomorphicDayJs(date).tz(timeZone).format(formatStr);
|
||||||
};
|
};
|
||||||
|
|
||||||
// returns the human-friendly difference between now and given date (e.g. "5 minutes", "9 months", etc.)
|
// returns the human-friendly difference between now and given date (e.g. "5 minutes", "9 months", etc.)
|
||||||
|
Reference in New Issue
Block a user