Files
cfw-autumn/shared/utils/common/formatUtils/formatUnix.ts
2026-01-07 13:49:51 +00:00

51 lines
1.1 KiB
TypeScript

import { format } from "date-fns/format";
export const formatMsToDate = (
unixDate?: number | null,
options?: { withTimezone?: boolean },
) => {
if (!unixDate) {
return "undefined";
}
return format(new Date(unixDate), "dd MMM yyyy");
};
export const formatMs = (
unixDate?: number | null | "now",
options?: { withTimezone?: boolean; excludeSeconds?: boolean },
) => {
if (unixDate === "now") {
return "now";
}
if (!unixDate) {
return "undefined";
}
let formatString = options?.excludeSeconds
? "dd MMM yyyy HH:mm"
: "dd MMM yyyy HH:mm:ss";
if (options?.withTimezone) {
formatString = `${formatString} z`;
}
return format(new Date(unixDate), formatString);
};
/**
* Formats a unix timestamp in SECONDS to a date and time string.
* If unixSeconds is falsy, returns "undefined unix date".
*/
export const formatSeconds = (
unixSeconds?: number | null,
options?: { withTimezone?: boolean },
): string => {
if (!unixSeconds && unixSeconds !== 0) {
return "undefined unix date";
}
return format(
new Date(unixSeconds * 1000),
options?.withTimezone ? "dd MMM yyyy HH:mm:ss z" : "dd MMM yyyy HH:mm:ss",
);
};