The example in this article describes how to simply convert String to Date using JS. Share it with everyone for your reference, the details are as follows:
<script> var s=["2008-8-1","2009/9/2","10/3/2010"]; for(var i=0;i<s.length;i++){ var d = string2date(s[i]); var year = d.getFullYear(); var month = d.getMonth()+1; var date = d.getDate(); var dateStr = year+" 年 "+month+" 月 "+ date+ " 日"; alert("原始串:"+s[i]+"\n直接转:"+ new Date(s[i])+"\n用方法转:"+dateStr); } function string2date(str){ return new Date(Date.parse(str.replace(/-/g, "/"))); } </script>
Readers who are interested in more JavaScript-related content can check out the special topics on this site: "Summary of JavaScript search algorithm techniques", "Summary of JavaScript animation special effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm techniques", "Summary of JavaScript traversal algorithms and techniques" and "JavaScript Mathematics Summary of operation usage》
I hope this article will be helpful to everyone in JavaScript programming.