(
dateString: string,
options: {
dateFormat?: string;
timeFormat?: string;
showTime?: boolean;
userTimeFormat?: "12" | "24"; // User's time format preference
} = {}
)
| 1071 | * Format a date/datetime string for display, showing time if available |
| 1072 | */ |
| 1073 | export function formatDateTimeForDisplay( |
| 1074 | dateString: string, |
| 1075 | options: { |
| 1076 | dateFormat?: string; |
| 1077 | timeFormat?: string; |
| 1078 | showTime?: boolean; |
| 1079 | userTimeFormat?: "12" | "24"; // User's time format preference |
| 1080 | } = {} |
| 1081 | ): string { |
| 1082 | if (!dateString) return ""; |
| 1083 | |
| 1084 | const { |
| 1085 | dateFormat = "MMM d, yyyy", |
| 1086 | timeFormat, |
| 1087 | showTime = true, |
| 1088 | userTimeFormat = "24", |
| 1089 | } = options; |
| 1090 | |
| 1091 | // Use userTimeFormat if no specific timeFormat is provided |
| 1092 | const finalTimeFormat = timeFormat || (userTimeFormat === "12" ? "h:mm a" : "HH:mm"); |
| 1093 | |
| 1094 | try { |
| 1095 | const parsed = parseDateToLocalInternal(dateString); |
| 1096 | const hasTime = hasTimeComponent(dateString); |
| 1097 | |
| 1098 | if (hasTime && showTime) { |
| 1099 | // Handle empty dateFormat case (e.g., for "Today at" scenarios) |
| 1100 | if (!dateFormat || dateFormat.trim() === "") { |
| 1101 | return format(parsed, finalTimeFormat); |
| 1102 | } else { |
| 1103 | return format(parsed, `${dateFormat} ${finalTimeFormat}`); |
| 1104 | } |
| 1105 | } else { |
| 1106 | // For date-only, return the dateFormat or fallback |
| 1107 | if (!dateFormat || dateFormat.trim() === "") { |
| 1108 | return ""; // Return empty for time-only requests when no time available |
| 1109 | } else { |
| 1110 | return format(parsed, dateFormat); |
| 1111 | } |
| 1112 | } |
| 1113 | } catch (error) { |
| 1114 | tasknotesLogger.error("Error formatting datetime for display:", { |
| 1115 | category: "validation", |
| 1116 | operation: "formatting-datetime-display", |
| 1117 | details: { dateString }, |
| 1118 | error: error, |
| 1119 | }); |
| 1120 | return dateString; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | /** |
| 1125 | * Time-aware comparison for before/after relationships with consistent UTC parsing |
no test coverage detected