fix: calculate age function

This commit is contained in:
puriphatt 2024-06-07 12:20:05 +00:00
parent 422cad2046
commit b3f04aa6cf

View file

@ -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<Pagination<User[]>>();
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() {