Skip to content

Commit

Permalink
Use relative dates (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggravlingen authored Dec 27, 2024
1 parent c680546 commit 97ef451
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
5 changes: 4 additions & 1 deletion frontend/src/GenericComponents/BasicTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export enum CellAlign {

export enum CellDataType {
DATE = "date",
DATE_RELATIVE = "date_relative",
NUMBER = "number",
PER_CENT = "per_cent",
STRING = "string",
Expand Down Expand Up @@ -83,7 +84,9 @@ function getCellValue(
}

if (columnSetting.dataType === CellDataType.DATE) {
return formatDate(extractedValue);
return formatDate(extractedValue, false);
} else if (columnSetting.dataType === CellDataType.DATE_RELATIVE) {
return formatDate(extractedValue, true);
} else if (columnSetting.dataType === CellDataType.NUMBER) {
return formatNumber(extractedValue, columnSetting.noDecimal ?? 1, false);
} else if (columnSetting.dataType === CellDataType.PER_CENT) {
Expand Down
16 changes: 15 additions & 1 deletion frontend/src/Utils/date.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";

dayjs.extend(relativeTime);

/**
* Formats a given date as a string in the format of "yyyy-mm-dd".
* If the input is `undefined`, returns `null`.
* @param dateInput - The date to format, or `undefined`.
* @param isRelative - If `true`, returns the relative time from now.
* @returns The formatted date as "yyyy-mm-dd", or `null` if input is `undefined`.
*/
export function formatDate(dateInput: Date | undefined): string | null {
export function formatDate(
dateInput: Date | undefined,
isRelative: boolean = false,
): string | null {
if (!dateInput) {
return null;
}

const date = new Date(dateInput);
if (isRelative) {
return dayjs(date).fromNow();
}

const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");

return `${year}-${month}-${day}`;
}
2 changes: 1 addition & 1 deletion frontend/src/components/TableMarketDataOverview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const columnSettings = [
headerName: "End",
fieldPath: "lastDate",
align: CellAlign.RIGHT,
dataType: CellDataType.DATE,
dataType: CellDataType.DATE_RELATIVE,
description: "The last date of the market data",
},
];
Expand Down

0 comments on commit 97ef451

Please sign in to comment.