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

标签 javascript jquery momentjs

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

这是警告错误:

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]

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

最佳答案

查看他们所有很棒的文档!

这是他们讨论 Warning Message 的地方.

字符串+格式

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.

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

String + Formats(多种格式)

如果您有不止一种格式,请查看它们的 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"]);

请查看文档以了解更具体的内容。

时区

结帐 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.

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

编辑

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

关于javascript - Moment.js 中的弃用警告 - 不是公认的 ISO 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969570/

相关文章:

javascript - 使用 javascript 监听事件

javascript - Qualtrics:jquery 附加的空按钮刷新页面

php - 日常风采网站

javascript - 在日期属性中按天按数组重新分组

javascript - 使用天/周和标题创建带有时刻的自定义日历

calendar - 全日历中的 Jalali 日历

javascript - 刷新chart.config.data

javascript - 当文本变得可见时,在滚动期间更改图像

javascript - 使用 jquery Replace() 标记添加函数

javascript - 使用上一页和下一页按钮制作我自己的滑动图片库