1

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.

handsome
  • 2,335
  • 7
  • 45
  • 73

3 Answers3

2

Tage a look Check if date is a valid one

you should not use moment as jsx element, it is an normal class.

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