refactor: Improve code readability by formatting function arguments and return values in date handling methods

This commit is contained in:
Nawaz Dhandala
2025-09-08 21:51:53 +01:00
parent 0aae7877c7
commit a55f2f7842
8 changed files with 40 additions and 16 deletions

View File

@@ -201,12 +201,18 @@ export default class OneUptimeDate {
return this.secondsToFormattedFriendlyTimeString(seconds);
}
public static toTimeString(date: Date | string, use12HourFormat?: boolean): string {
public static toTimeString(
date: Date | string,
use12HourFormat?: boolean,
): string {
if (typeof date === "string") {
date = this.fromString(date);
}
const format = use12HourFormat || this.getUserPrefers12HourFormat() ? "hh:mm A" : "HH:mm";
const format =
use12HourFormat || this.getUserPrefers12HourFormat()
? "hh:mm A"
: "HH:mm";
return moment(date).format(format);
}
@@ -890,11 +896,14 @@ export default class OneUptimeDate {
// Server-side: default to 12-hour format for user-friendly display
return true;
}
// Client-side: detect user's preferred time format from browser locale
const testDate = new Date();
const timeString = testDate.toLocaleTimeString();
return timeString.toLowerCase().includes('am') || timeString.toLowerCase().includes('pm');
return (
timeString.toLowerCase().includes("am") ||
timeString.toLowerCase().includes("pm")
);
}
public static getDateAsUserFriendlyFormattedString(
@@ -1113,7 +1122,8 @@ export default class OneUptimeDate {
let date: string | Date = data.date;
const onlyShowDate: boolean | undefined = data.onlyShowDate;
let timezones: Array<Timezone> | undefined = data.timezones;
const use12HourFormat: boolean = data.use12HourFormat ?? this.getUserPrefers12HourFormat();
const use12HourFormat: boolean =
data.use12HourFormat ?? this.getUserPrefers12HourFormat();
date = this.fromString(date);

View File

@@ -55,7 +55,10 @@ const EventHistoryDayList: FunctionComponent<ComponentProps> = (
width: isMobile ? "100%" : "15%",
}}
>
{OneUptimeDate.getDateAsUserFriendlyLocalFormattedString(props.date, true)}
{OneUptimeDate.getDateAsUserFriendlyLocalFormattedString(
props.date,
true,
)}
</div>
<div
style={{

View File

@@ -144,7 +144,8 @@ const LogItem: FunctionComponent<ComponentProps> = (
DATE TIME:
</div>
<div className="text-slate-500 courier-prime">
{OneUptimeDate.getDateAsUserFriendlyFormattedString(props.log.time)} &nbsp;{" "}
{OneUptimeDate.getDateAsUserFriendlyFormattedString(props.log.time)}{" "}
&nbsp;{" "}
</div>
</div>
)}

View File

@@ -88,7 +88,9 @@ const CustomMonitorSummaryView: FunctionComponent<ComponentProps> = (
title="Monitored At"
value={
props.monitoredAt
? OneUptimeDate.getDateAsUserFriendlyLocalFormattedString(props.monitoredAt)
? OneUptimeDate.getDateAsUserFriendlyLocalFormattedString(
props.monitoredAt,
)
: "-"
}
/>

View File

@@ -175,7 +175,11 @@ const TraceExplorer: FunctionComponent<ComponentProps> = (
</div>
<div className="">
<div className="font-semibold">Seen at:</div>{" "}
<div>{OneUptimeDate.getDateAsUserFriendlyFormattedString(span.startTime!)}</div>
<div>
{OneUptimeDate.getDateAsUserFriendlyFormattedString(
span.startTime!,
)}
</div>
</div>
<div className="">
<div className="font-semibold">Start:</div>{" "}

View File

@@ -209,7 +209,9 @@ export const getIncidentEventItem: GetIncidentEventItemFunction = (
anotherStatus: incident.incidentSeverity?.name,
eventSecondDescription: incident.createdAt
? "Created at " +
OneUptimeDate.getDateAsUserFriendlyLocalFormattedString(incident.createdAt!)
OneUptimeDate.getDateAsUserFriendlyLocalFormattedString(
incident.createdAt!,
)
: "",
eventTypeColor: Red,
labels:

View File

@@ -419,9 +419,10 @@ RunCron(
})
.join(", ") || "",
scheduledAt: OneUptimeDate.getDateAsUserFriendlyFormattedString(
event.startsAt!,
),
scheduledAt:
OneUptimeDate.getDateAsUserFriendlyFormattedString(
event.startsAt!,
),
eventTitle: event.title || "",
eventDescription: event.description || "",
unsubscribeUrl: unsubscribeUrl,

View File

@@ -381,9 +381,10 @@ RunCron(
scheduledEventStateTimeline.scheduledMaintenanceState
?.name || "",
scheduledAt: OneUptimeDate.getDateAsUserFriendlyFormattedString(
event.startsAt!,
),
scheduledAt:
OneUptimeDate.getDateAsUserFriendlyFormattedString(
event.startsAt!,
),
eventTitle: event.title || "",
unsubscribeUrl: unsubscribeUrl,
subscriberEmailNotificationFooterText: