UNPKG

@mquiuqui/doc-validator

Version:

Validador de documentos brasileiros (CPF, CNPJ, etc)

32 lines (31 loc) 1.19 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isValidDateBR = isValidDateBR; exports.isOver18 = isOver18; exports.getAge = getAge; function isValidDateBR(date) { const [day, month, year] = date.split('/').map(Number); const dt = new Date(year, month - 1, day); return (dt.getFullYear() === year && dt.getMonth() === month - 1 && dt.getDate() === day); } function isOver18(date) { const [day, month, year] = date.split('/').map(Number); const birthDate = new Date(year, month - 1, day); const today = new Date(); const age = today.getFullYear() - birthDate.getFullYear(); const m = today.getMonth() - birthDate.getMonth(); return age > 18 || (age === 18 && m >= 0 && today.getDate() >= birthDate.getDate()); } function getAge(date) { const [day, month, year] = date.split('/').map(Number); const birthDate = new Date(year, month - 1, day); const today = new Date(); let age = today.getFullYear() - birthDate.getFullYear(); const m = today.getMonth() - birthDate.getMonth(); if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { age--; } return age; }