Skip to content
Advertisement

Convert string to date format in JavaScript

I have a string variable I want the variable into date format.

My Code:

JavaScript

My Output:

JavaScript

Expected Output:

JavaScript

Advertisement

Answer

You can use toLocaleDateString with an argument that specifies a locale for which your format is used. For instance, the Swedisch locale uses the YYYY-MM-DD format:

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