mirror of
https://github.com/dbeaver/cloudbeaver.git
synced 2026-09-24 16:04:36 +08:00
dbeaver/pro#8089 validate date before formatting (#4091)
* dbeaver/pro#8089 validate date before formatting * dbeaver/pro#8089 treat date and time the same --------- Co-authored-by: Daria Marutkina <125263541+dariamarutkina@users.noreply.github.com>
This commit is contained in:
+20
-12
@@ -16,6 +16,10 @@ import { DateTimeKind, useFormattingContext } from '../../FormattingContext.js';
|
||||
import { TableDataContext } from '../../TableDataContext.js';
|
||||
import type { ICellFormatterProps } from '../ICellFormatterProps.js';
|
||||
|
||||
function isValidDate(date: Date): boolean {
|
||||
return !isNaN(date.getTime());
|
||||
}
|
||||
|
||||
export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTimeFormatter() {
|
||||
const tableDataContext = useContext(TableDataContext);
|
||||
const formattingContext = useFormattingContext();
|
||||
@@ -34,12 +38,13 @@ export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTime
|
||||
return <GridNullFormatter />;
|
||||
}
|
||||
|
||||
let value = displayValue;
|
||||
let date = new Date(displayValue);
|
||||
let dateFormatter: Intl.DateTimeFormat | null = null;
|
||||
|
||||
if (formattingContext.formatters) {
|
||||
const extendedDateKind = formattingContext.getExtendedDateKind(cellContext.cell.column);
|
||||
|
||||
let dateFormatter: Intl.DateTimeFormat | null = null;
|
||||
|
||||
switch (extendedDateKind) {
|
||||
case DateTimeKind.DateTime:
|
||||
dateFormatter = formattingContext.formatters.dateTime;
|
||||
@@ -51,19 +56,22 @@ export const DateTimeFormatter = observer<ICellFormatterProps>(function DateTime
|
||||
dateFormatter = formattingContext.formatters.dateOnly;
|
||||
break;
|
||||
}
|
||||
if (dateFormatter) {
|
||||
if (DateTimeKind.TimeOnly === extendedDateKind) {
|
||||
const [h = 0, m = 0, s = 0] = displayValue.split(':').map(Number);
|
||||
const date = new Date();
|
||||
date.setHours(h, m, s, 0);
|
||||
value = dateFormatter.format(date);
|
||||
} else {
|
||||
const date = new Date(displayValue);
|
||||
value = dateFormatter.format(date);
|
||||
}
|
||||
|
||||
if (DateTimeKind.TimeOnly === extendedDateKind) {
|
||||
const [h = 0, m = 0, s = 0] = displayValue.split(':').map(Number);
|
||||
const time = new Date();
|
||||
time.setHours(h, m, s, 0);
|
||||
|
||||
date = time;
|
||||
}
|
||||
}
|
||||
|
||||
let value = displayValue;
|
||||
|
||||
if (dateFormatter && isValidDate(date)) {
|
||||
value = dateFormatter.format(date);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="tw:flex tw:items-center tw:overflow-hidden">
|
||||
<div className="tw:overflow-hidden tw:text-ellipsis">{value}</div>
|
||||
|
||||
Reference in New Issue
Block a user