JS simple implementation of String to Date method

  • 2021-01-19 22:02:31
  • OfStack

This article describes the JS simple implementation of String to Date method. To share with you for your reference, 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+"  years  "+month+"  month  "+ date+ "  day ";
  alert(" The original list: "+s[i]+"\n Go directly: "+ new Date(s[i])+"\n To transfer by means of: "+dateStr);
}
function string2date(str){
  return new Date(Date.parse(str.replace(/-/g,  "/")));
}
</script>

More about JavaScript related content interested readers can view the site features: "JavaScript search algorithm skills summary", "JavaScript animation effects and skills summary", "JavaScript error and debugging skills summary", "JavaScript data structure and algorithm skills summary", "JavaScript eraser algorithm and skills summary" and "JavaScript mathematical operation usage summary"

I hope this article is helpful to JavaScript program design.


Related articles: