Home > Enterprise >  Calculate birthdate from year, month and days
Calculate birthdate from year, month and days

Time:11-15

I'm working on a calculator that needs to calculate the date of birth from 3 inputs: the age in years, months and days:

    const arrDatas = [];
    const elmDataNasc = document.querySelector('#dataNasc');

    function setDataAnos (e) { // store at position 0 of arrDates the years

        arrDatas[0] = e;
        setDataNasc();

    }

    function setDataMeses (e) { // store in position 1 of arrDatas the months

        arrDatas[1] = e;
        setDataNasc();

    }

    function setDataDias (e) { // store in position 2 of arrDatas the days

        arrDatas[2] = e;
        setDataNasc();

    }

    function setDataNasc () {

        let anosDias = arrDatas[0] * 365; // convert years to days
        let mesesDias = arrDatas[1] * 30.417; // convert months to days (30,417 is the average of days per month)
        let diasDias = arrDatas[2]; // Days

        let totalDias = anosDias   mesesDias   diasDias; // add the total number of days and assign it to a variable

        let dataData = new Date(); // current date

        dataData.setDate( dataData.getDate() - totalDias); // subtract total days from the current date

        let data = dataData.toLocaleDateString("pt-BR", { year: "numeric", month: "2-digit", day: "2-digit",}); // format to DD/MM/YYYY

        elmDataNasc.value = data; // assign date to birthdate input
        
    }
<form>
            <p>Enter data to calculate date of birth:</p>

            <label for="years">Years</label>
            <input onchange="setDataAnos(this.value)" type="number" name="years" min="0" max="31">

            <label for="months">Months</label>
            <input onchange="setDataMeses(this.value)" type="number" name="months" min="0" max="11">

            <label for="days">Days</label>
            <input onchange="setDataDias(this.value)" type="number" name="days" min="0" max="31">
            <br>
            <br>
            <label for="dateBirth">Date of birth:</label>
            <input id="dataNasc" type="text" name="dataNasc" value="" disabled>
        </form>

This script only allows you to calculate an approximate date, as it does not take into account the exact number of days in each month or leap years. I would like her to calculate the exact date of birth but I have no idea how to do that, I've racked my brains and searched the internet but nothing. Any light?

CodePudding user response:

Here's a naive approach you should be able to adapt:

let arrDatas = [39, 4, 18];

const isLeapYear = year => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;
const daysInYear = year => 365   (isLeapYear(year) ? 1 : 0);
const daysInMonth = (year, month) => [31, 28   (isLeapYear(year) ? 1 : 0), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];

let when = new Date();
let year = when.getFullYear();
let month = when.getMonth();

let totalDias = 0;

for(let i = 0; i < arrDatas[0]; i  )
{
    year--;
    totalDias  = daysInYear(year);
}
for(let i = 0; i < arrDatas[1]; i  )
{
    month--;
    totalDias  = daysInMonth(year, month);
}
totalDias  = arrDatas[2];

let dataData = new Date();
dataData.setDate(dataData.getDate() - totalDias);

console.log("You were born on", dataData);

You can sub in your own values instead of the dummy arrDatas in the code above.

This doesn't take into account things like timezone changes and leap seconds.

Also, you will want to validate your input. Think about someone entering 20 years, 3 months and 5383 days.

CodePudding user response:

First subtract the number of days from the date now to give a new date.

Then subtract number of months from that date to give a new date.

Then subtract number of years from that date to give the date you want:

const days = 22;
const months = 4;
const years = 63;

let d = new Date(Date.now());
console.log(d);
d.setDate(d.getDate() - days);
console.log(d);
d.setMonth(d.getMonth() - months);
console.log(d);
d.setFullYear(d.getFullYear() - years);
console.log(d);

  • Related