[英]validate date with Moment in React
is there a way to display a default message when the date passed to Moment is invalid?当传递给 Moment 的日期无效时,有没有办法显示默认消息?
<Moment fromNow>not-a-date</Moment>
I´m getting Invalid Date .我得到了无效的日期。
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()
}
Tage a look Check if date is a valid one 看一下日期是否有效
you should not use moment as jsx element, it is an normal class. 您不应该使用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.