Home  >  Article  >  Web Front-end  >  How to convert JavaScript string to date

How to convert JavaScript string to date

藏色散人
藏色散人Original
2021-05-11 11:22:4842141browse

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.

How to convert JavaScript string to date

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.

How to convert JavaScript string to date

In the js tag, store the time string in the str variable.

How to convert JavaScript string to date

In the js tag, use the replace() method to replace "-" with "/", which is used to obtain the time object below.

How to convert JavaScript string to date

Inside the js tag, use new Date() to convert the string to date format.

How to convert JavaScript string to date

In the js tag, use the write() method to output the conversion result.

How to convert JavaScript string to date

Open the test.html file in the browser to view the results.

How to convert JavaScript string to date

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn