dev: hotfix, improvements
+ components/pub/my-ui/data-table + components/pub/ui/badge + lib/date
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
export function getAge(dateString: string, comparedDate?: string): { idFormat: string; extFormat: string } {
|
||||
const birthDate = new Date(dateString);
|
||||
const today = new Date();
|
||||
|
||||
if (comparedDate) {
|
||||
const comparedDateObj = new Date(comparedDate);
|
||||
today.setFullYear(comparedDateObj.getFullYear());
|
||||
today.setMonth(comparedDateObj.getMonth());
|
||||
today.setDate(comparedDateObj.getDate());
|
||||
}
|
||||
|
||||
// Format the date part
|
||||
const options: Intl.DateTimeFormatOptions = { day: 'numeric', month: 'long', year: 'numeric' };
|
||||
const idFormat = birthDate.toLocaleDateString('id-ID', options);
|
||||
|
||||
// Calculate age
|
||||
let years = today.getFullYear() - birthDate.getFullYear();
|
||||
let months = today.getMonth() - birthDate.getMonth();
|
||||
let days = today.getDate() - birthDate.getDate();
|
||||
|
||||
if (months < 0 || (months === 0 && days < 0)) {
|
||||
years--;
|
||||
months += 12;
|
||||
}
|
||||
|
||||
if (days < 0) {
|
||||
const prevMonth = new Date(today.getFullYear(), today.getMonth() - 1, 0);
|
||||
days += prevMonth.getDate();
|
||||
months--;
|
||||
}
|
||||
|
||||
// Format the age part
|
||||
let extFormat = '';
|
||||
if ([years, months, days].filter(Boolean).join(' ')) {
|
||||
extFormat = `${years} Tahun ${months} Bulan ${days} Hari`;
|
||||
} else {
|
||||
extFormat = '0';
|
||||
}
|
||||
|
||||
return {
|
||||
idFormat,
|
||||
extFormat
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user