Skip to content

Commit 21dca7a

Browse files
author
ALIANE LYDIA
committed
feat(annotation): update date format
OpenSILEX/opensilex-dev!1377
1 parent dfb4a74 commit 21dca7a

2 files changed

Lines changed: 26 additions & 3 deletions

File tree

opensilex-front/front/src/components/annotations/list/AnnotationList.vue

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ export default class AnnotationList extends Vue {
142142
@Ref("annotationModalForm") readonly annotationModalForm!: AnnotationModalForm;
143143
144144
formatDate(dateStr: string): string {
145-
const localeString = new Date(dateStr).toLocaleString();
146-
147-
return localeString.replace(/\//g, '-');
145+
return this.$opensilex.$dateTimeFormatter.formatLocalFixedDateTime(dateStr);
148146
}
149147
150148
static getDefaultColumns() {

opensilex-front/front/src/models/DateTimeFormatter.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,31 @@ export default class DateTimeFormatter {
9595
.format(new Date(dateTimeValue));
9696
}
9797

98+
/**
99+
* Formats a date string into a local date and time string with the format `YYYY-MM-DD HH:mm:ss`.
100+
* The output uses the local timezone of the environment where the code runs.
101+
*
102+
* @param dateStr - The date string to format.
103+
* @returns The formatted date and time string in the format `YYYY-MM-DD HH:mm:ss`.
104+
* Returns an empty string if the input is invalid or empty.
105+
*/
106+
formatLocalFixedDateTime(dateStr: string): string {
107+
if (!dateStr) return "";
108+
109+
const date = new Date(dateStr);
110+
if (isNaN(date.getTime())) return ""; // invalid date check
111+
112+
const year = date.getFullYear();
113+
const month = String(date.getMonth() + 1).padStart(2, '0');
114+
const day = String(date.getDate()).padStart(2, '0');
115+
116+
const hours = String(date.getHours()).padStart(2, '0');
117+
const minutes = String(date.getMinutes()).padStart(2, '0');
118+
const seconds = String(date.getSeconds()).padStart(2, '0');
119+
120+
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
121+
}
122+
98123
/**
99124
* Formats a period between two dates
100125
*

0 commit comments

Comments
 (0)