From c2f6d53e5153024400f677bdc73ca2bd4e89c1da Mon Sep 17 00:00:00 2001 From: puriphatt Date: Mon, 10 Jun 2024 11:22:45 +0000 Subject: [PATCH] fix: add cal age to utils store --- src/stores/utils/index.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/stores/utils/index.ts b/src/stores/utils/index.ts index 9457f733..181391a3 100644 --- a/src/stores/utils/index.ts +++ b/src/stores/utils/index.ts @@ -1,5 +1,6 @@ import { Dialog } from 'quasar'; import GlobalDialog from 'components/GlobalDialog.vue'; +import { useI18n } from 'vue-i18n'; export function dialog(opts: { title: string; @@ -17,3 +18,23 @@ export function dialog(opts: { componentProps: opts, }); } + +export function calculateAge(birthDate: Date | null | string) { + const { locale } = useI18n(); + + 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 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' : ''} `; + } +}