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

下面是警告错误:

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

当前回答

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

我采用了与其他评分答案相似的方法。我注意到我不需要“。format”。此方法用于从moment(new Date(dateObjectVariable))构造函数创建一个moment对象,以便为我的函数操作它。

其他回答

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

Const moment = require('moment');

的时刻。suppressDeprecationWarnings = true;

你可以使用

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

当date不是ISO格式时,这应该被使用,因为它会告诉moment我们当前的格式是什么。

我使用moment将日期值转换为我想要的格式。 数据库中的日期值为

2021 - 06 - 07 - t22:00:00.000z

我所做的如下:

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

参考资料如下: https://momentjs.com/docs/#/parsing/string-format/

我得到了同样的问题,我使用下面的片段,它为我工作

moment(date, moment.ISO_8601)

谢谢你!

在我的例子中,这个错误发生在

moment('2021-07-1')

正确的方法是

moment('2021-07-01')

当月/日小于10时,需要在前面加0。