handsome
handsome

Reputation: 2422

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.

Upvotes: 1

Views: 1862

Answers (3)

Purva Tayde
Purva Tayde

Reputation: 1

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

Upvotes: 0

Holger D. Schauf
Holger D. Schauf

Reputation: 147

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

Upvotes: 2

Holger D. Schauf
Holger D. Schauf

Reputation: 147

Tage a look Check if date is a valid one

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

Upvotes: 2

Related Questions