Think I'm ready to host this one!
This commit is contained in:
@@ -19,18 +19,40 @@ export const ccn = ({
|
||||
return twMerge(className, context ? on : off);
|
||||
};
|
||||
|
||||
export const formatTime = (timestamp: string) => {
|
||||
const date = new Date(timestamp);
|
||||
const time = date.toLocaleTimeString('en-US', {
|
||||
type Timestamp = number | string | Date;
|
||||
|
||||
const toDate = (ts: Timestamp): Date | null => {
|
||||
if (ts instanceof Date) return isNaN(ts.getTime()) ? null : ts;
|
||||
|
||||
if (typeof ts === 'number') {
|
||||
// Heuristic: treat small numbers as seconds
|
||||
const ms = ts < 1_000_000_000_000 ? ts * 1000 : ts;
|
||||
const d = new Date(ms);
|
||||
return isNaN(d.getTime()) ? null : d;
|
||||
}
|
||||
|
||||
// string: try numeric first, then ISO/date string
|
||||
const asNum = Number(ts);
|
||||
const d =
|
||||
Number.isFinite(asNum) && asNum !== 0 ? toDate(asNum) : new Date(ts);
|
||||
|
||||
return d && !isNaN(d.getTime()) ? d : null;
|
||||
};
|
||||
|
||||
export const formatTime = (timestamp: Timestamp, locale = 'en-US'): string => {
|
||||
const date = toDate(timestamp);
|
||||
if (!date) return '--:--';
|
||||
return date.toLocaleTimeString(locale, {
|
||||
hour: 'numeric',
|
||||
minute: 'numeric',
|
||||
});
|
||||
return time;
|
||||
};
|
||||
|
||||
export const formatDate = (timestamp: string) => {
|
||||
const date = new Date(timestamp);
|
||||
const day = date.getDate();
|
||||
const month = date.toLocaleString('default', { month: 'long' });
|
||||
return `${month} ${day}`;
|
||||
export const formatDate = (timestamp: Timestamp, locale = 'en-US'): string => {
|
||||
const date = toDate(timestamp);
|
||||
if (!date) return '--/--';
|
||||
return date.toLocaleDateString(locale, {
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
});
|
||||
};
|
||||
|
Reference in New Issue
Block a user