diff --git a/src/stores/user/index.ts b/src/stores/user/index.ts index ce102803..6456f130 100644 --- a/src/stores/user/index.ts +++ b/src/stores/user/index.ts @@ -14,6 +14,7 @@ import { import axios from 'axios'; import useBranchStore from '../branch'; import { Branch } from '../branch/types'; +import { useI18n } from 'vue-i18n'; const branchStore = useBranchStore(); @@ -66,17 +67,24 @@ const useUserStore = defineStore('api-user', () => { }); const data = ref>(); + const { locale } = useI18n(); - function calculateAge(birthDate: Date | null): string { - if (!birthDate) return ''; + function calculateAge(birthDate: Date | null | string) { + if (!birthDate) return null; const birthDateTimeStamp = new Date(birthDate).getTime(); const now = new Date(); const diff = now.getTime() - birthDateTimeStamp; const ageDate = new Date(diff); const years = ageDate.getUTCFullYear() - 1970; - const result = `${years} ปี`; - return result; + const months = ageDate.getUTCMonth(); + const days = ageDate.getUTCDate() - 1; + + if (locale.value === 'th-th') { + return `${years} ปี ${months !== 0 ? months + ' เดือน' : ''} ${days !== 0 ? days + ' วัน' : ''} `; + } else { + return `${years} years ${months !== 0 ? months + ' months' : ''} ${days !== 0 ? days + ' days' : ''} `; + } } async function fetchHqOption() {