fix: add cal age to utils store

This commit is contained in:
puriphatt 2024-06-10 11:22:45 +00:00
parent 227c1bafe0
commit c2f6d53e51

View file

@ -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' : ''} `;
}
}