Skip to content

Commit 2dcb91d

Browse files
committed
feat: shared date formatters in spa utils respecting datesFormat/timeFormat settings
1 parent bf72252 commit 2dcb91d

4 files changed

Lines changed: 56 additions & 25 deletions

File tree

‎adminforth/spa/src/components/ValueRenderer.vue‎

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@
115115

116116
<script setup lang="ts">
117117
118-
import dayjs from 'dayjs';
119-
import utc from 'dayjs/plugin/utc';
120-
import timezone from 'dayjs/plugin/timezone';
121-
import {checkEmptyValues} from '@/utils';
118+
import {checkEmptyValues, formatDate, formatDateTime, formatTime} from '@/utils';
122119
import { useRoute, useRouter } from 'vue-router';
123120
import "vue3-json-viewer/dist/vue3-json-viewer.css";
124121
import { computed, defineAsyncComponent } from 'vue';
@@ -131,10 +128,6 @@ import type { any } from 'zod';
131128
const coreStore = useCoreStore();
132129
const route = useRoute();
133130
134-
135-
dayjs.extend(utc);
136-
dayjs.extend(timezone);
137-
138131
const props = defineProps<{
139132
column: any,
140133
record: any
@@ -150,21 +143,6 @@ const canShowForeignRecord = computed(() => {
150143
)?.allowedActions?.show;
151144
});
152145
153-
function formatDateTime(date: string) {
154-
if (!date) return '';
155-
return dayjs.utc(date).local().format(`${coreStore.config?.datesFormat} ${coreStore.config?.timeFormat}` || 'YYYY-MM-DD HH:mm:ss');
156-
}
157-
158-
function formatDate(date: string) {
159-
if (!date) return '';
160-
return dayjs.utc(date).local().format(coreStore.config?.datesFormat || 'YYYY-MM-DD');
161-
}
162-
163-
function formatTime(time: string) {
164-
if (!time) return '';
165-
return dayjs(`0000-00-00 ${time}`).format(coreStore.config?.timeFormat || 'HH:mm:ss');
166-
}
167-
168146
function getArrayItemDisplayValue(value: any, column: AdminForthResourceColumnCommon) {
169147
if (column.isArray?.itemType === 'datetime') {
170148
return formatDateTime(value);

‎adminforth/spa/src/utils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './utils/utils';
22
export * from './utils/listUtils';
3-
export * from './utils/createEditUtils';
3+
export * from './utils/createEditUtils';
4+
export * from './utils/dateUtils';
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import dayjs from 'dayjs';
2+
import utc from 'dayjs/plugin/utc';
3+
import timezone from 'dayjs/plugin/timezone';
4+
import { useCoreStore } from '../stores/core';
5+
6+
dayjs.extend(utc);
7+
dayjs.extend(timezone);
8+
9+
export const DEFAULT_DATES_FORMAT = 'YYYY-MM-DD';
10+
export const DEFAULT_TIME_FORMAT = 'HH:mm:ss';
11+
12+
type DateLike = string | number | Date | null | undefined;
13+
14+
/**
15+
* Date format configured in customization.datesFormat (or a sane default).
16+
*/
17+
export function getDatesFormat(): string {
18+
return useCoreStore().config?.datesFormat || DEFAULT_DATES_FORMAT;
19+
}
20+
21+
/**
22+
* Time format configured in customization.timeFormat (or a sane default).
23+
*/
24+
export function getTimeFormat(): string {
25+
return useCoreStore().config?.timeFormat || DEFAULT_TIME_FORMAT;
26+
}
27+
28+
/**
29+
* Formats UTC datetime into user's local time using formats from customization settings.
30+
* Use it instead of toLocaleString() everywhere, so all dates in admin look the same.
31+
*/
32+
export function formatDateTime(date: DateLike): string {
33+
if (!date) return '';
34+
return dayjs.utc(date).local().format(`${getDatesFormat()} ${getTimeFormat()}`);
35+
}
36+
37+
/**
38+
* Formats date using format from customization settings.
39+
*/
40+
export function formatDate(date: DateLike): string {
41+
if (!date) return '';
42+
return dayjs.utc(date).local().format(getDatesFormat());
43+
}
44+
45+
/**
46+
* Formats standalone time value (e.g. "13:45:00") using format from customization settings.
47+
*/
48+
export function formatTime(time: string | null | undefined): string {
49+
if (!time) return '';
50+
return dayjs(`0000-00-00 ${time}`).format(getTimeFormat());
51+
}

‎adminforth/spa/src/utils/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './utils';
22
export * from './listUtils';
3-
export * from './createEditUtils';
3+
export * from './createEditUtils';
4+
export * from './dateUtils';

0 commit comments

Comments
 (0)