Moment: how to get a person's age

Created on 12 Feb 2013  ·  8Comments  ·  Source: moment/moment

Hi, this is more a question then an issue but here it go.
I want to use moment to check if a person is more then 18 years old. What is the best way to do this? Since if i use the .fromNow() function i will get 18 years if the person has more then 17 and a half.

Most helpful comment

You can use something like birthday.diff(moment(), 'years') for this, where birthday is a moment of the person's birthday.

All 8 comments

You can use something like birthday.diff(moment(), 'years') for this, where birthday is a moment of the person's birthday.

Ok it's working. Thanks a lot.

const age = moment().diff(birthday, 'years');
const isLegal = (age >= 18);

Math.abs(birthday.diff(moment(), 'years')) with Math.abs you convert this result in a positive number

You can use something like birthday.diff(moment(), 'years') for this, where birthday is a moment of the person's birthday.

this works but it gives a warning on console, any idea why?

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments: 
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: Fri Sep 27 2019 00:00:00 GMT+0100 (Western European Summer Time), _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.eval [as createFromInputFallback] (webpack-internal:///./node_modules/moment/moment.js:319:98)
    at configFromString (webpack-internal:///./node_modules/moment/moment.js:2384:15)
    at configFromInput (webpack-internal:///./node_modules/moment/moment.js:2610:13)
    at prepareConfig (webpack-internal:///./node_modules/moment/moment.js:2593:13)
    at createFromConfig (webpack-internal:///./node_modules/moment/moment.js:2560:44)
    at createLocalOrUTC (webpack-internal:///./node_modules/moment/moment.js:2647:16)
    at createLocal (webpack-internal:///./node_modules/moment/moment.js:2651:16)
    at hooks (webpack-internal:///./node_modules/moment/moment.js:11:29)
    at VueComponent.Singup (webpack-internal:///./node_modules/cache-loader/dist/cjs.js?!./node_modules/babel-loader/lib/index.js!./node_modules/cache-loader/dist/cjs.js?!./node_modules/vue-loader/lib/index.js?!./src/views/Singup.vue?vue&type=script&lang=js&:95:63)
    at invokeWithErrorHandling (webpack-internal:///./node_modules/vue/dist/vue.runtime.esm.js:1853:26)

@josebedoya That wouldn't be necessary if you just swapped the relative calculations like @eballeste did:

moment().diff(birthdate, 'years')

It still doesn't work correctly if the person has recently (less than a month ago) had their birthday.

It still doesn't work correctly if the person has recently (less than a month ago) had their birthday.

moment('2020-05-07').diff(moment('2002-05-06'), 'years') returns 18

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dogukankotan picture dogukankotan  ·  3Comments

IbraheemAlSaady picture IbraheemAlSaady  ·  3Comments

vbullinger picture vbullinger  ·  3Comments

dbshwang picture dbshwang  ·  3Comments

RobinvanderVliet picture RobinvanderVliet  ·  3Comments