GRAB X APOTEK X Hospital PORTAL
This commit is contained in:
@@ -11,6 +11,40 @@ export function fDateTime(date: Date | string | number) {
|
||||
return format(new Date(date), 'dd MMM yyyy HH:mm');
|
||||
}
|
||||
|
||||
// Function to calculate the age from the birthdate
|
||||
function calculateAge(birthDate: Date): number {
|
||||
const today = new Date();
|
||||
const age = today.getFullYear() - birthDate.getFullYear();
|
||||
const monthDifference = today.getMonth() - birthDate.getMonth();
|
||||
const dayDifference = today.getDate() - birthDate.getDate();
|
||||
|
||||
// Adjust age if birthday hasn't occurred yet this year
|
||||
if (monthDifference < 0 || (monthDifference === 0 && dayDifference < 0)) {
|
||||
return age - 1;
|
||||
}
|
||||
|
||||
return age;
|
||||
}
|
||||
|
||||
// Extended function to format the date and include the age if it's a birthdate
|
||||
export function fDateTimeWithAge(date: Date | string | number): string {
|
||||
const birthDate = new Date(date);
|
||||
const formattedDate = format(birthDate, 'dd MMM yyyy');
|
||||
const age = calculateAge(birthDate);
|
||||
|
||||
// If the age is 0, the output should be months instead of years
|
||||
let ageText = age > 0 ? `${age} years old` : '';
|
||||
|
||||
if (age === 0) {
|
||||
const monthsOld = new Date().getMonth() - birthDate.getMonth() +
|
||||
(12 * (new Date().getFullYear() - birthDate.getFullYear()));
|
||||
ageText = `${monthsOld} months old`;
|
||||
}
|
||||
|
||||
return `${formattedDate} / ${ageText}`;
|
||||
}
|
||||
|
||||
|
||||
export function fTimestamp(date: Date | string | number) {
|
||||
return getTime(new Date(date));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user