OneCompiler

[Javascript] How can I create a date object from string?

I want to create a date object from a given string, How to do that in Javascript?

1 Answer

4 years ago by

You can use new Date() which converts string to date. For examlpe, check the below code and result

let str = '2021/12/31';

let dateStr = new Date(str);

console.log(dateStr); 

Run here https://onecompiler.com/javascript/3xnrkhy4e

4 years ago by Meera