簡體   English   中英

Moment.js 中的棄用警告 - 不是公認的 ISO 格式

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

我收到一條警告,指出提供給 moment 的值不是公認的 ISO 格式。 我今天用 function 時刻更改了我的變量,但它仍然不起作用。

這是警告錯誤:

棄用警告:提供的值不是公認的 ISO 格式。 moment 構造回退到 js Date(),這在所有瀏覽器和版本中都不可靠。 不鼓勵使用非 ISO 日期格式,並將在即將發布的主要版本中將其刪除。 請參閱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]

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

查看他們所有很棒的文檔!

這是他們討論警告信息的地方。

字符串 + 格式

警告:瀏覽器對解析字符串的支持不一致。 因為沒有關於應該支持哪些格式的規范,所以在某些瀏覽器中有效的內容在其他瀏覽器中無效。

對於解析除 ISO 8601 字符串以外的任何內容的一致結果,您應該使用String + Format

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

字符串 + 格式(多種格式)

如果您有不止一種格式,請查看他們的String + Formats (帶有“s”)。

如果您不知道輸入字符串的確切格式,但知道它可能是眾多格式之一,則可以使用格式數組。

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

請查看文檔以獲取更具體的內容。

時區

Checkout Parsing in Zone ,時區的等效文檔。

moment.tz 構造函數采用與 moment 構造函數相同的所有參數,但使用最后一個參數作為時區標識符。

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

這樣做對我有用:

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

像這樣在你的函數中使用時刻

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

我遇到了這個錯誤,因為我試圖從localStorage傳遞一個日期。 將日期傳遞給一個新的Date對象,然后調用.toISOString()對我有用:

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

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

您可以使用

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

這應該在日期不是 ISO 格式時使用,因為它會告訴我們當前的格式是什么。

這個答案是為了更好地理解這個警告

使用 moment 創建時間對象時會引起棄用警告, var today = moment(); .

如果這個警告對你沒問題,那么我有一個更簡單的方法。

不要使用js中的date對象,而是使用moment 例如使用moment()來獲取當前日期。

或者將js日期對象轉換為moment日期。 您可以簡單地指定js日期對象的格式。

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

例如:

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

(但moment只能使用這種方法直到它被貶值,這可能會在未來貶值)

用 moment.js 解析字符串。

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

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

const moment = require('moment');

moment.suppressDeprecationWarnings = true;

一個簡單的答案:

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

就我而言,此錯誤發生在

moment('2021-07-1')

正確的方法是

moment('2021-07-01')

當月/日小於10時,需要在前面加0

我正在使用時刻將我的日期值轉換為我想要的格式。 數據庫中的日期值就像

2021-06-07T22:00:00.000Z

我所做的如下:

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

參考在這里: https ://momentjs.com/docs/#/parsing/string-format/

這可能有點過時了。 我的問題是我根本沒有從數據庫傳遞的格式(我的是 NULL )。

文檔說: 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().

我遇到了類似的問題並通過以下解決方案解決:我的日期格式是:'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;

 var nowTime = moment();

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

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

這種語法幫助我避免警告

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

我遇到了同樣的問題,我在下面使用了代碼片段,它對我有用

moment(date, moment.ISO_8601)

謝謝

在此處輸入圖像描述

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