【问题标题】:Moment.js formatting outputs as 'invalid date'Moment.js 格式化输出为“无效日期”
【发布时间】:2015-04-16 19:44:28
【问题描述】:

我有一个使用 moment.js 的 formatTime 函数 但如果数字作为 NaN moment.js 输出为“无效日期”

我该如何解决这个问题?

$scope.formatTime = function(time) {

    if (time>24){
        time-=24;
    }
    return moment({
        hour: Math.floor(time),
        minute: time - Math.floor(time) * 60
    }).format('h:mm a');
};

【问题讨论】:

    标签: angularjs ionic-framework momentjs


    【解决方案1】:

    尝试使用 Moment 的 isValid() 方法,如果日期无效,则采取不同的做法。例如,这将返回 false:

    moment("not a real date").isValid()
    

    文档说明这是针对 1.7+ 版本的,验证的详细信息可在 in the Moment documentation 获得。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-06
      • 2020-01-16
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      • 2014-06-17
      • 2020-11-13
      相关资源
      最近更新 更多