getAge scripts

node v10.21.0
version: 2.0.0
endpointsharetweet
var moment = require("moment") const allDateFormats = [ 'MM/DD/YYYY', 'DD/MM/YYYY', 'YYYY/MM/DD', moment.ISO_8601, ]; function getAgeOld(date, reference = new Date()) { const compare = moment(new Date(date)); const diff = moment(reference).diff(compare, 'years', true); return Math.round(diff * 10) / 10; }; function getAge(dateOfBirth, today = new Date()) { const dob = moment.utc(dateOfBirth, allDateFormats) .startOf('day'); return moment.utc(moment(today)) .startOf('day') .diff(dob, 'years', true) .toFixed(3); }; const dob = '1977/09/03'; const oldAge = getAgeOld(dob); const newAge = getAge(dob); console.log('oldAge: ' + oldAge); console.log('newAge: ' + newAge);
Loading…

no comments

    sign in to comment