fix: future birth date

This commit is contained in:
puriphatt 2024-07-16 03:23:45 +00:00
parent 608f84b0df
commit 204a854a1c
2 changed files with 35 additions and 5 deletions

View file

@ -1,9 +1,11 @@
<script setup lang="ts">
import useUserStore from 'src/stores/user';
import useOptionStore from 'src/stores/options';
import { dateFormat } from 'src/utils/datetime';
import {
dateFormat,
calculateAge,
disabledAfterToday,
} from 'src/utils/datetime';
const userStore = useUserStore();
const optionStore = useOptionStore();
const firstName = defineModel<string>('firstName');
@ -13,7 +15,7 @@ const lastNameEN = defineModel<string>('lastNameEN');
const telephoneNo = defineModel<string>('telephoneNo');
const email = defineModel<string>('email');
const gender = defineModel<string>('gender');
const birthDate = defineModel<Date | null>('birthDate');
const birthDate = defineModel<Date | string | null>('birthDate');
const nationality = defineModel<string>('nationality');
defineProps<{
@ -123,6 +125,7 @@ defineProps<{
utc
autoApply
v-model="birthDate"
:disabled-dates="disabledAfterToday"
:teleport="true"
:dark="$q.dark.isActive"
:locale="$i18n.locale === 'th-th' ? 'th' : 'en'"
@ -168,7 +171,7 @@ defineProps<{
readonly
:label="$t('formDialogInputAge')"
class="col-3"
:model-value="birthDate ? userStore.calculateAge(birthDate) : ''"
:model-value="birthDate ? calculateAge(birthDate) : ''"
/>
<q-select
v-if="employee"

View file

@ -1,6 +1,7 @@
import moment from 'moment';
import 'moment/dist/locale/th';
import 'moment/dist/locale/en-gb';
import { useI18n } from 'vue-i18n';
moment.locale('en-gb');
@ -76,3 +77,29 @@ export function toISOStringWithTimezone(date: Date) {
getTimezoneOffset(date)
);
}
export function calculateAge(birthDate: Date | null | string) {
if (!birthDate) return null;
const { locale } = useI18n();
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' : ''} `;
}
}
export function disabledAfterToday(date: Date) {
const today = new Date();
today.setHours(0, 0, 0, 0);
return date > today;
}