简体   繁体   English

Moment.js 中的弃用警告 - 不是公认的 ISO 格式

[英]Deprecation warning in Moment.js - Not in a recognized ISO format

I'm getting a warning that a value provided to moment is not in a recognized ISO format.我收到一条警告,指出提供给 moment 的值不是公认的 ISO 格式。 I changed my variable today with the moment function and still it doesn't work.我今天用 function 时刻更改了我的变量,但它仍然不起作用。

Here's the warning error:这是警告错误:

Deprecation warning: value provided is not in a recognized ISO format.弃用警告:提供的值不是公认的 ISO 格式。 moment construction falls back to js Date(), which is not reliable across all browsers and versions. moment 构造回退到 js Date(),这在所有浏览器和版本中都不可靠。 Non ISO date formats are discouraged and will be removed in an upcoming major release.不鼓励使用非 ISO 日期格式,并将在即将发布的主要版本中将其删除。 Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.请参阅http://momentjs.com/guides/#/warnings/js-date/了解更多信息。 Arguments: [0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object] Arguments: [0] _isAMomentObject: true, _isUTC: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object]

var entryDate = new Date();
var currentDate = entryDate.getDate();

function between(x, min, max) {
  return x.valueOf() >= min.valueOf() && x < max.valueOf();
}

$('#custom1').change(function () {
  if ($('#custom1 :selected').val() == 'AU') {
    var keyword = '';

    var aus1_s = moment.tz('2016-9-26 19:30', 'Australia/Sydney');
    var aus2_s = moment.tz('2016-10-2 19:30', 'Australia/Sydney');
    var aus3_s = moment.tz('2016-10-9 19:30', 'Australia/Sydney');
    var aus4_s = moment.tz('2016-10-16 19:30', 'Australia/Sydney');
    var aus5_s = moment.tz('2016-10-23 19:30', 'Australia/Sydney');
    var aus6_s = moment.tz('2016-10-30 19:30', 'Australia/Sydney');
    var aus6_e = moment.tz('2016-11-5 19:30', 'Australia/Sydney');
  } else if ($('#custom1 :selected').val() == 'NZ') {
    var aus1_s = moment.tz('2016-9-28 20:30', 'Pacific/Auckland');
    var aus2_s = moment.tz('2016-10-4 20:30', 'Pacific/Auckland');
    var aus3_s = moment.tz('2016-10-11 20:30', 'Pacific/Auckland');
    var aus4_s = moment.tz('2016-10-18 20:30', 'Pacific/Auckland');
    var aus5_s = moment.tz('2016-10-25 20:30', 'Pacific/Auckland');
    var aus6_s = moment.tz('2016-11-2 20:30', 'Pacific/Auckland');
    var aus6_e = moment.tz('2016-11-9 20:30', 'Pacific/Auckland');
  } else {
    $('#entryEquals').val('');
    return false;
  }

  var today = moment();

  switch (true) {
    case between(today, aus1_s, aus2_s):
      keyword = 'RElYT04=';
      break;

    case between(today, aus2_s, aus3_s):
      keyword = 'QlJJREU=';
      break;

    case between(today, aus3_s, aus4_s):
      keyword = 'U1lETkVZ';
      break;

    case between(today, aus4_s, aus5_s):
      keyword = 'R1JPT00=';
      break;

    case between(today, aus5_s, aus6_s):
      keyword = 'V0VERElORw==';
      break;

    case between(today, aus6_s, aus6_e):
      keyword = 'VExD';
      break;

    default:
      $('#entryEquals').val('');
      break;
  }

  $('#entryEquals').val(keyword);
});

Check out all their awesome documentation!查看他们所有很棒的文档!

Here is where they discuss the Warning Message .这是他们讨论警告信息的地方。

String + Format字符串 + 格式

Warning: Browser support for parsing strings is inconsistent.警告:浏览器对解析字符串的支持不一致。 Because there is no specification on which formats should be supported, what works in some browsers will not work in other browsers.因为没有关于应该支持哪些格式的规范,所以在某些浏览器中有效的内容在其他浏览器中无效。

For consistent results parsing anything other than ISO 8601 strings, you should use String + Format .对于解析除 ISO 8601 字符串以外的任何内容的一致结果,您应该使用String + Format

moment("12-25-1995", "MM-DD-YYYY");

String + Formats (multiple formats)字符串 + 格式(多种格式)

If you have more than one format, check out their String + Formats (with an 's').如果您有不止一种格式,请查看他们的String + Formats (带有“s”)。

If you don't know the exact format of an input string, but know it could be one of many, you can use an array of formats.如果您不知道输入字符串的确切格式,但知道它可能是众多格式之一,则可以使用格式数组。

moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);

Please checkout the documentation for anything more specific.请查看文档以获取更具体的内容。

Timezone时区

Checkout Parsing in Zone , the equivalent documentation for timezones. Checkout Parsing in Zone ,时区的等效文档。

The moment.tz constructor takes all the same arguments as the moment constructor, but uses the last argument as a time zone identifier. moment.tz 构造函数采用与 moment 构造函数相同的所有参数,但使用最后一个参数作为时区标识符。

var b = moment.tz("May 12th 2014 8PM", "MMM Do YYYY hA", "America/Toronto");

EDIT编辑

//...
var dateFormat = "YYYY-M-D H:m"; //<-------- This part will get rid of the warning.
var aus1_s, aus2_s, aus3_s, aus4_s, aus5_s, aus6_s, aus6_e;
if ($("#custom1 :selected").val() == "AU" ) {
    var region = 'Australia/Sydney';

    aus1_s = moment.tz('2016-9-26 19:30', dateFormat, region);              
    aus2_s = moment.tz('2016-10-2 19:30', dateFormat, region);              
    aus3_s = moment.tz('2016-10-9 19:30', dateFormat, region);                  
    aus4_s = moment.tz('2016-10-16 19:30', dateFormat, region);                 
    aus5_s = moment.tz('2016-10-23 19:30', dateFormat, region);
    aus6_s = moment.tz('2016-10-30 19:30', dateFormat, region);
    aus6_e = moment.tz('2016-11-5 19:30', dateFormat, region);
} else if ($("#custom1 :selected").val() == "NZ" ) {
    var region = 'Pacific/Auckland';

    aus1_s =  moment.tz('2016-9-28 20:30', dateFormat, region);
    aus2_s =  moment.tz('2016-10-4 20:30', dateFormat, region);
    aus3_s =  moment.tz('2016-10-11 20:30', dateFormat, region);
    aus4_s =  moment.tz('2016-10-18 20:30', dateFormat, region);
    aus5_s =  moment.tz('2016-10-25 20:30', dateFormat, region);
    aus6_s =  moment.tz('2016-11-2 20:30', dateFormat, region);
    aus6_e =  moment.tz('2016-11-9 20:30', dateFormat, region);
}
//...

这样做对我有用:

moment(new Date("27/04/2016")).format

像这样在你的函数中使用时刻

 moment(new Date(date)).format('MM/DD/YYYY')

I ran into this error because I was trying to pass in a date from localStorage .我遇到了这个错误,因为我试图从localStorage传递一个日期。 Passing the date into a new Date object, and then calling .toISOString() did the trick for me:将日期传递给一个新的Date对象,然后调用.toISOString()对我有用:

const dateFromStorage = localStorage.getItem('someDate');
const date = new Date(dateFromStorage);
const momentDate = moment(date.toISOString());

This suppressed any warnings in the console.这抑制了控制台中的任何警告。

You can use您可以使用

moment(date,"currentFormat").format("requiredFormat");

This should be used when date is not ISO Format as it'll tell moment what our current format is.这应该在日期不是 ISO 格式时使用,因为它会告诉我们当前的格式是什么。

This answer is to give a better understanding of this warning这个答案是为了更好地理解这个警告

Deprecation warning is caused when you use moment to create time object, var today = moment();使用 moment 创建时间对象时会引起弃用警告, var today = moment(); . .

If this warning is okay with you then I have a simpler method.如果这个警告对你没问题,那么我有一个更简单的方法。

Don't use date object from js use moment instead.不要使用js中的date对象,而是使用moment For example use moment() to get the current date.例如使用moment()来获取当前日期。

Or convert the js date object to moment date.或者将js日期对象转换为moment日期。 You can simply do that specifying the format of your js date object.您可以简单地指定js日期对象的格式。

ie, moment("js date", "js date format");moment("js date", "js date format");

eg:例如:

moment("2014 04 25", "YYYY MM DD");

(BUT YOU CAN ONLY USE THIS METHOD UNTIL IT'S DEPRECIATED, this may be depreciated from moment in the future) (但moment只能使用这种方法直到它被贬值,这可能会在未来贬值)

Parsing string with moment.js.用 moment.js 解析字符串。

const date = '1231231231231' //Example String date
const parsed = moment(+date);

在代码中添加以下行以抑制警告:

const moment = require('moment');

moment.suppressDeprecationWarnings = true;

A simple answer:一个简单的答案:

let date = Date.now();
let timeNow = moment(new Date(date)).format('YYYY-MM-DD');

In my case, this error happens when就我而言,此错误发生在

moment('2021-07-1')

The correct way is正确的方法是

moment('2021-07-01')

When the month/date is less than 10 , you need to add 0 in front.当月/日小于10时,需要在前面加0

I was using moment to convert my date value to the format I want.我正在使用时刻将我的日期值转换为我想要的格式。 The date value from the database is like数据库中的日期值就像

2021-06-07T22:00:00.000Z 2021-06-07T22:00:00.000Z

What I did is as below:我所做的如下:

dateNeeded = moment(dateNeeded , moment.ISO_8601).format('YYYY-MM-DD');

The reference is here: https://momentjs.com/docs/#/parsing/string-format/参考在这里: https ://momentjs.com/docs/#/parsing/string-format/

This may be a bit overdue.这可能有点过时了。 My issue was I did not have a format at all being passed from the database ( mine was NULL ).我的问题是我根本没有从数据库传递的格式(我的是 NULL )。

The docs say: This deprecation warning is thrown when no known format is found for a date passed into the string constructor. To work around this issue, specify a format for the string being passed to moment().文档说: This deprecation warning is thrown when no known format is found for a date passed into the string constructor. To work around this issue, specify a format for the string being passed to moment(). This deprecation warning is thrown when no known format is found for a date passed into the string constructor. To work around this issue, specify a format for the string being passed to moment().

I have similar issue faced and solve with following solution: my date format is: 'Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)'我遇到了类似的问题并通过以下解决方案解决:我的日期格式是:'Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)'

let currentDate = moment(new Date('Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)').format('DD-MM-YYYY'); // 'Fri Dec 11 2020 05:00:00 GMT+0500 (Pakistan Standard Time)'

let output=(moment(currentDate).isSameOrAfter('07-12-2020'));
const dateFormat = 'MM-DD-YYYY';

const currentDateStringType = moment(new Date()).format(dateFormat);
    
const currentDate = moment(new Date() ,dateFormat);  // use this 

moment.suppressDeprecationWarnings = true; moment.suppressDeprecationWarnings = true;

 var nowTime = moment();

 var nowTimeFormate = moment(nowTime).format("HH:mm");

 console.log('from now time',nowTimeFormate)

this syntax help me to avoid warning这种语法帮助我避免警告

let m = moment(new Date(dateObjectVariable));

I was getting the same issue, I used below snippet, it worked for me我遇到了同样的问题,我在下面使用了代码片段,它对我有用

moment(date, moment.ISO_8601)

Thank you谢谢

在此处输入图像描述

import moment from 'moment';


 console.log("________________22 __ " + moment(new Date(), 'MM/DD/YYYY').toDate() );
                console.log("________________22 __ " + moment(new Date()).format("DD-MM-YYYY") );
                console.log("________________22 __ " + moment(new Date()).format("DD-MMM-YYYY") );

谢谢christo8989,这解决了我的问题。

moment("12-25-1995", ["MM-DD-YYYY", "YYYY-MM-DD"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM