繁体   English   中英

使用 Moment in React 验证日期

[英]validate date with Moment in React

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

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

我得到了无效的日期

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

看一下日期是否有效

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

getDate() {
    if (!this.props.date || this.props.date===" " || check for invalid date criteria) 
{
        return "Alternative Message on Invalid date"
    }

    return  <Moment fromNow>{this.props.date}</Moment>
}

render() {
    return this.getDate()
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM