在计算年龄差异时收到警告?

getting warning while calculating difference in age?

我在计算年龄差异时出现警告

这是我的代码 http://plnkr.co/edit/1wIvVISmgEqcRNnAD971?p=preview

moment.min.js:1 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: 09-Feb-1983, _f: undefined, _strict: undefined, _locale: [object Object] Error at Function.createFromInputFallback (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:3368) at Yt (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:21353) at Ot (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:22064) at Tt (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:22146) at Nt (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:24464) at M.ln.diff (https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js:1:29987) at Object. (http://run.plnkr.co/rVHCD6iVYofYv9Th/app.js:8:22) at Object.invoke (https://code.angularjs.org/1.4.12/angular.js:4570:17) at extend.instance (https://code.angularjs.org/1.4.12/angular.js:9435:34) at nodeLinkFn (https://code.angularjs.org/1.4.12/angular.js:8540:34

我是这样做的

let DOB= "09-Feb-1983"
console.log(moment(DOB, 'DD-MMM-YYYY', true).isValid())
console.log(moment().format('DD-MMM-YYYY'));
console.log(moment().diff('09-Feb-1983', 'years'))

如何删除这个警告我已经指定了格式

您可以在将字符串传递给 diff() 函数之前将其转换为 moment。

这样试试

let DOB= "09-Feb-1983"
console.log(moment(DOB, 'DD-MMM-YYYY', true).isValid())
console.log(moment().format('DD-MMM-YYYY'));
console.log(moment().diff(moment('09-Feb-1983', 'DD-MMM-YYYY'), 'years'));