Home > Article > Web Front-end > How to convert JavaScript string to date
How to convert JavaScript string to date: first create a new html file; then store the time string in the str variable in the js tag; then use the "replace()" method to replace "-" is "/"; finally use "new Date()" to convert the string into date format.
The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.
Create a new html file, named test.html, to explain how js converts strings into dates.
In the js tag, store the time string in the str variable.
In the js tag, use the replace() method to replace "-" with "/", which is used to obtain the time object below.
Inside the js tag, use new Date() to convert the string to date format.
In the js tag, use the write() method to output the conversion result.
Open the test.html file in the browser to view the results.
Recommended study: "javascript advanced tutorial"
The above is the detailed content of How to convert JavaScript string to date. For more information, please follow other related articles on the PHP Chinese website!