【问题标题】:Removing deprecation warning from momentjs从 momentjs 中删除弃用警告
【发布时间】:2017-02-06 20:02:18
【问题描述】:

当用户以M/D/YYYY 格式输入日期以恢复为MM/DD/YYYY 格式(例如2/5/201702/05/2017)时,我正在使用momentjs 在我的项目中处理日期。我还将任何无效日期或大于今天的日期转换回今天的日期。

element.on("blur", function() {
    var currentDate = moment().format('MM/DD/YYYY');
    var formattedInput;

    if (ctrl.$modelValue !== undefined && ctrl.$modelValue !== "") {

        if(moment(ctrl.$modelValue, "MM/DD/YYYY", true).isValid()) {
            formattedInput = moment(ctrl.$modelValue);
            formattedInput.format('MM/DD/YYYY');
            if (formattedInput.isAfter(currentDate)) {
                ctrl.$setViewValue(currentDate);
                ctrl.$render();
            }
        } else if (moment(ctrl.$modelValue, "M/D/YYYY", true).isValid()) {
            formattedInput = moment(ctrl.$modelValue);
            formattedInput.format('MM/DD/YYYY');
            if (formattedInput.isAfter(currentDate)) {
                ctrl.$setViewValue(currentDate);
                ctrl.$render();
            } else {
                ctrl.$setViewValue(formattedInput.format('MM/DD/YYYY'));
                ctrl.$render();
            }
        } else {
            ctrl.$setViewValue(currentDate);
            ctrl.$render();
        }
    }
});

据我所知,我上面的代码一切正常。但无论工作功能如何,我都会收到非 ISO 日期的弃用警告。我的想法是使用MM/DD/YYYY 格式,但是由于业务需求,这是不可更改的。有没有办法以不麻烦的方式解决这个问题?

【问题讨论】:

    标签: momentjs


    【解决方案1】:

    问题在于formattedInput = moment(ctrl.$modelValue) 在这里您使用非 ISO 日期格式的时刻解析。要删除 弃用警告,只需使用 moment(ctrl.$modelValue, "MM/DD/YYYY")moment(ctrl.$modelValue, "M/D/YYYY"),就像在 if 条件中所做的那样。

    您的完整代码如下:

    element.on("blur", function() {
      var currentDate = moment();
      var formattedInput;
    
      if (ctrl.$modelValue !== undefined && ctrl.$modelValue !== "") {
    
        if(moment(ctrl.$modelValue, "MM/DD/YYYY", true).isValid()) {
          formattedInput = moment(ctrl.$modelValue, "MM/DD/YYYY", true);
          // This line returns a string, but does not assign to value, so it's superfluous
          //formattedInput.format('MM/DD/YYYY');
          if (formattedInput.isAfter(currentDate)) {
             ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
             ctrl.$render();
          }
        } else if (moment(ctrl.$modelValue, "M/D/YYYY", true).isValid()) {
          formattedInput = moment(ctrl.$modelValue, "M/D/YYYY", true);
          // see previous comment
          //formattedInput.format('MM/DD/YYYY');
          if (formattedInput.isAfter(currentDate)) {
            ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
            ctrl.$render();
          } else {
            ctrl.$setViewValue(formattedInput.format('MM/DD/YYYY'));
            ctrl.$render();
          }
        } else {
          ctrl.$setViewValue(currentDate.format('MM/DD/YYYY'));
          ctrl.$render();
        }
      }
    });
    

    请务必完全理解moment parsing(从字符串构建矩对象)和矩format(显示矩对象的字符串表示)之间的区别。

    【讨论】:

    • 哇,我没有注意到 .format() 没有返回时刻对象。这最终成为问题的全部根本原因,因为我的today 变量在我的.isAfter() 调用中被比较为字符串与时刻对象。我已经通过更改var currentDate = moment() 来更改代码以使其工作,并且在设置视图值时仅使用.format() 函数。非常感谢!
    • 不客气!我没有注意到 today var 被视为字符串,即使您已经解决了您的问题,我已经在您发表评论后修复了我的代码示例。
    猜你喜欢
    • 2018-03-31
    • 2019-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2017-08-23
    • 2020-07-20
    • 1970-01-01
    • 2017-05-25
    相关资源
    最近更新 更多