简体   繁体   中英

Convert String Format to Date Format JS

I have one String-formatted Datetime.

I want to give an error if a value has been selected in the 12-hour interval after the current date. However, I do not know how to change data.

I used the following line of code.

// this.startDate0 = Input value (string) (Value: 31/07/2020 20:00)
console.log(Date.parse(this.startDate0)); // Maybe NaN maybe 1581099600000

I did not understand how to do it, can you help?

If you're open to using a library, I'd suggest using the date-fns parse function. Supply the expected pattern as a parameter and it will parse the value. You can then use the helper differenceInHours method to compare with 12 hours from present.

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