简体   繁体   中英

How fix Invalid date in JS?

Need format my date When I try do following:

 var d = new Date('15.01.2019'); console.log(d); 

I get 'Invalid Date' message If there is other date, for example '01.15.2019' all is correct How can I solve this problem?

The constructor new Date(dateString) uses the Date.parse() method for parsing the date string. According to documentation of this method , you should use an ISO 8601 compliant date, or a simplification of ISO 8601, which in your case would be YEAR-MONTH-DAY, for example, 2015-01-15.

Other formats may work but are not reliable, as per the documentation:

"Other formats are accepted, but results are implementation-dependent."

It means that using date strings that are not ISO 8601 compliant or simplifications may result in different behaviour across different JavaScript engines and web browsers. It may also vary with the user set's locale.

Use an ISO 8601 simplified string and you will be fine.

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