| | |
| | | export function filterNumbersFromString(str: string) { |
| | | return str.replace(/\D/g, ''); |
| | | } |
| | | |
| | | export function calculateAge(idCardNumber) { |
| | | if (BoleRegExp.RegIDCard.test(idCardNumber)) { |
| | | const birthDateStr = idCardNumber.match(/(\d{6})(\d{8})/)[2]; |
| | | const birthDate = new Date( |
| | | birthDateStr.substring(0, 4), |
| | | birthDateStr.substring(4, 6) - 1, |
| | | birthDateStr.substring(6, 8) |
| | | ); |
| | | |
| | | // Calculate age |
| | | const today = new Date(); |
| | | const age = |
| | | today.getFullYear() - |
| | | birthDate.getFullYear() - |
| | | (today.getMonth() < birthDate.getMonth() || |
| | | (today.getMonth() === birthDate.getMonth() && today.getDate() < birthDate.getDate()) |
| | | ? 1 |
| | | : 0); |
| | | |
| | | return age; |
| | | } |
| | | return null; |
| | | } |