こんにちは、これは問題というよりも質問ですが、ここにあります。
瞬間を使って18歳以上かどうかを確認したいと思います。 これを行うための最良の方法は何ですか? .fromNow()関数を使用すると、17歳半以上の人がいる場合は18年になります。
これにはbirthday.diff(moment(), 'years')
ようなものを使用できます。ここで、 birthday
はその人の誕生日の瞬間です。
動作しています。 どうもありがとう。
const age = moment().diff(birthday, 'years');
const isLegal = (age >= 18);
Math.abs(birthday.diff(moment(), 'years'))
Math.absを使用すると、この結果を正の数に変換できます
これには
birthday.diff(moment(), 'years')
ようなものを使用できます。ここで、birthday
はその人の誕生日の瞬間です。
これは機能しますが、コンソールに警告が表示されます。理由は何ですか。
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)
あなただけの@eballesteのような相対的な計算がしたスワップ場合に必要ではないでしょう@josebedoya:
moment().diff(birthdate, 'years')
その人が最近(1か月以内に)誕生日を迎えた場合でも、正しく機能しません。
その人が最近(1か月以内に)誕生日を迎えた場合でも、正しく機能しません。
moment('2020-05-07').diff(moment('2002-05-06'), 'years')
は18
返します
最も参考になるコメント
これには
birthday.diff(moment(), 'years')
ようなものを使用できます。ここで、birthday
はその人の誕生日の瞬間です。