I have a string variable I want the variable into date format.
My Code:
JavaScript
x
3
1
var date = '2/3/2022 17:57:30'
2
var temp = new Date(date)
3
My Output:
JavaScript
1
2
1
2022-02-03T17:57:30.000Z
2
Expected Output:
JavaScript
1
4
1
2022-02-03
2
3
4
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
1
3
1
var date = '2/3/2022 17:57:30'
2
var temp = new Date(date).toLocaleDateString("se");
3
console.log(temp);