Momentjs : How to prevent “Invalid date”?
问题 I have the following code : var fomattedDate = moment(myDate).format("L"); Sometimes moment(myDate).format("L") returns "Invalid date", I want to know if there is a way to prevent that and return an empty string instead. 回答1: TL;DR If your goal is to find out whether you have a valid date, use Moment's isValid : var end_date_moment, end_date; jsonNC.end_date = jsonNC.end_date.replace(" ", "T"); end_date_moment = moment(jsonNC.end_date); end_date = end_date_moment.isValid() ? end_date_moment