【问题标题】:How to avoid 'deprecation waring' on moment.js?如何避免 moment.js 上的“弃用警告”?
【发布时间】:2020-02-03 15:30:16
【问题描述】:

我有多种格式的日期数据,如下所示。

  1. 20-02-01 22:12:13
  2. 2020-02-01(周五)22:12:13
  3. 2020/02/01(周五)22:12:13
  4. 2020/02/01 22:12

我想将这些格式数据细化为 'YYYY/DD/MM hh:mm' 格式。

因此我将那些未精炼的数据放到 moment.js 中!

date = moment(date).format('YYYY-DD-MM HH:mm');

然后时刻在下面给出了这个警告。

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: 2020/02/03 22:52:28, _f: undefined, _strict: undefined, _locale: [object Object]
Error
    at Function.createFromInputFallback (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:320:98)
    at configFromString (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:2385:15)
    at configFromInput (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:2611:13)
    at prepareConfig (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:2594:13)
    at createFromConfig (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:2561:44)
    at createLocalOrUTC (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:2648:16)
    at createLocal (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:2652:16)
    at hooks (/Users/loganlee/project/portfolio/back_stastics/backend/node_modules/moment/moment.js:12:29)
    at /Users/loganlee/project/portfolio/back_stastics/backend/src/PreProcessor/index.js:263:15
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

我可以手动进行细化,但是...我必须细化日期数据以放入 moment() 并将数据细化为 ISO 或 RFC2822 格式以将其放入 moment() 的步骤也感觉很浪费。

请告诉我是否可以明智地使用 moment.js!

提前谢谢你!

【问题讨论】:

  • "要解决此问题,请指定传递给 moment() 的字符串的格式。" - 例如 moment($("#date").val(), 'YYYY-MM-DD hh:mm:ss a') - 您需要指定格式 当你解析它时
  • 如果我必须指定我给 moment() 的日期格式,我该如何处理多个未优化的日期数据?如果我有 4 种不同格式的日期数据,那么我需要使用 switch 吗?

标签: javascript momentjs


【解决方案1】:

无论您输入任何格式的数据,都可以避免该警告

  • 20-02-01 22:12:13
  • 2020-02-01(周五)22:12:13
  • 2020/02/01(周五)22:12:13
  • 2020/02/01 22:12

以下代码没有弃用警告。

date = moment(date,'YYYY-DD-MM HH:mm').format('YYYY-DD-MM HH:mm');

我不能确定这种方法是最好的解决方法,但除非您不需要在放入 moment() 之前优化数据!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-27
    • 2017-03-23
    • 2015-01-11
    • 2019-02-21
    • 1970-01-01
    • 2016-12-25
    • 2021-02-04
    • 1970-01-01
    相关资源
    最近更新 更多