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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39969570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:06:19  来源:igfitidea点击:

Deprecation warning in moment js - Not in a recognized ISO format

javascriptjquerymomentjs

提问by Jemai

I'm getting a warning that a value provided to moment is not in a recognized ISO format. I changed my variable today with the moment function and still it doesn't work.

我收到一条警告,指出提供给 moment 的值不是公认的 ISO 格式。我今天用 moment 函数更改了我的变量,但它仍然不起作用。

Here's the warning error:

这是警告错误:

Deprecation warning: value provided is not in a recognized ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non 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: true, _useUTC: true, _l: undefined, _i: 2016-9-26 19:30, _f: undefined, _strict: undefined, _locale: [object Object]

弃用警告:提供的值不是公认的 ISO 格式。moment 构造回退到 js Date(),这在所有浏览器和版本中都不可靠。不鼓励非 ISO 日期格式,并将在即将发布的主要版本中删除。请参阅http://momentjs.com/guides/#/warnings/js-date/了解更多信息。参数:[0] _isAMomentObject:真,_isUTC:真,_useUTC:真,_l:未定义,_i:2016-9-26 19:30,_f:未定义,_strict:未定义,_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);

        });

回答by christo8989

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);
}
//...

回答by Jigar Bhatt

Doing this works for me:

这样做对我有用:

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

回答by MD. ABU TALHA

use moment in your function like this

像这样在你的函数中使用 moment

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

回答by Shahzeb Khan

I ran into this error because I was trying to pass in a date from localStorage. Passing the date into a new Dateobject, and then calling .toISOString()did the trick for me:

我遇到了这个错误,因为我试图从localStorage. 将日期传递给一个新Date对象,然后调用.toISOString()对我有用

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

This suppressed any warnings in the console.

这抑制了控制台中的任何警告。

回答by Saahithyan Vigneswaran

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 dateobject from jsuse momentinstead. For example use moment()to get the current date.

不要使用dateobject from jsusemoment来代替。例如用于moment()获取当前日期。

Or convert the jsdate object to momentdate. You can simply do that specifying the format of your jsdate object.

或者将js日期对象转换为moment日期。您可以简单地指定js日期对象的格式。

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

IE, 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 momentin the future)

(但您只能使用这种方法直到它被贬值,这可能会moment在未来贬值)

回答by Коля Ракета

Parsing string with moment.js.

使用 moment.js 解析字符串。

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

回答by BeaST 30

Use 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 格式时使用,因为它会告诉我们当前的格式是什么。