gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 01:30:48 28 4
gpt4 key购买 nike

我在计算年龄差异时变得谨慎

这是我的代码 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() 函数之前将其转换为一个时刻。

尝试这样

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'));

关于javascript - 计算年龄差异时收到警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53296566/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com