Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to get a person's age #627

Closed
rollbackpt opened this issue Feb 12, 2013 · 9 comments
Closed

how to get a person's age #627

rollbackpt opened this issue Feb 12, 2013 · 9 comments

Comments

@rollbackpt
Copy link

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.

@timrwood
Copy link
Member

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

@rollbackpt
Copy link
Author

Ok it's working. Thanks a lot.

@eballeste
Copy link

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

@josebedoya
Copy link

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

@adouz
Copy link

adouz commented Sep 30, 2019

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)

@shrmnk
Copy link

shrmnk commented Mar 12, 2020

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

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

@andres-arista
Copy link

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

@ByScripts
Copy link

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

@RapunzlFounder
Copy link

By adding the optional third parameter, you can receive a float back which will be less than 18 if you try:
moment('2020-05-07').diff(moment('2002-05-06'), 'years', true) where you are sure to include true after 'years'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants