我得到一个警告,提供给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);
});

当前回答

我使用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/

其他回答

我遇到了这个错误,因为我试图从localStorage传递一个日期。将日期传递到一个新的date对象中,然后调用.toISOString()为我完成了这个技巧:

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

这将抑制控制台中任何警告。

使用moment.js解析字符串。

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

这个答案是为了更好地理解这个警告

当你使用moment来创建time对象,var today = moment();时,会产生弃用警告。

如果这个警告对你来说是好的,那么我有一个更简单的方法。

不要从js中使用date对象,而是使用moment。例如,使用moment()获取当前日期。

或者将js date对象转换为时刻日期。你可以简单地指定你的js date对象的格式。

即时刻(“js日期”,“js日期格式”);

eg:

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

(但你只能使用这种方法直到它贬值,这可能从未来的某一刻开始贬值)

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

像这样在函数中使用矩

let date = "01/11/1992"  // string
moment(new Date(date)).format('MM/DD/YYYY')