简体   繁体   中英

validate date with Moment in React

is there a way to display a default message when the date passed to Moment is invalid?

<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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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