【问题标题】:validate date with Moment in React使用 Moment in React 验证日期
【发布时间】:2019-10-29 14:31:36
【问题描述】:

当传递给 Moment 的日期无效时,有没有办法显示默认消息?

<Moment fromNow>not-a-date</Moment>

我收到无效日期

【问题讨论】:

    标签: reactjs momentjs


    【解决方案1】:
    getDate() {
        if (!this.props.date || check for invalid date) {
            return "Alternative Message on Invalid date"
        }
    
        return  <Moment fromNow>{this.props.date}</Moment>
    }
    
    render() {
        return this.getDate()
    }
    

    【讨论】:

      【解决方案2】:

      标记一下 Check if date is a valid one

      你不应该使用moment作为jsx元素,它是一个普通的类。

      【讨论】:

      猜你喜欢
      • 2018-05-12
      • 2018-08-30
      • 2020-09-04
      • 2021-02-06
      • 2018-11-25
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多