Skip to content
Advertisement

Convert date with forward slashes and partial time to Javascript date time

I have a date in a string format that looks like so:

JavaScript

… and I want to change it to a valid Javascript date and time.

I’ve tried changing the forward slashes to '-' with this code:

JavaScript

but it returns '31-07-2022 16:00' which is still an invalid date time. How can I convert this into a valid Date and Time so that I can use it to compare two dates programmatically?

Advertisement

Answer

The problem here is that you are using time in European format (DD/MM/YYYY), while JavaScript compiling it as American time format (MM/DD/YYYY), Here is a snippet that switch days and months to create a valid date

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement