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,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;
}