fix: add cal age to utils store
This commit is contained in:
parent
227c1bafe0
commit
c2f6d53e51
1 changed files with 21 additions and 0 deletions
|
|
@ -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' : ''} `;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue