Home > Web Front-end > JS Tutorial > body text

Introduction to the method of converting String to Date using JS

怪我咯
Release: 2017-07-04 15:15:52
Original
2325 people have browsed it

This article mainly introduces the simple method of converting String to Date using JS, involving techniques related to converting JavaScript string and date. Friends in need can refer to the following

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>
Copy after login

The above is the detailed content of Introduction to the method of converting String to Date using JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!