简体   繁体   中英

Preventing invalid date in momentjs

I have this code :

var dt = moment(cdt).format("L");

At some dates moment(cdt).format("L") returns Invalid date , So how can I return an empty string instead invalid date .

With moment.js you can check if a date is valid with the method .isValid() , for instance:

moment(cdt, 'L', true).isValid()

moment("12/13/2020", "DD/MM/YYYY", true).isValid() should return False .

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