JS字符串类型转换成日期类型的方法

<script type="text/javascript">
//字符串转日期格式,strDate要转为日期格式的字符串
function getDate(strDate){
  var date = eval('new Date(' + strDate.replace(/\d+(?=-[^-]+$)/, 
   function (a) { return parseInt(a, 10) - 1; }).match(/\d+/g) + ')');
  return date;
}
//测试
console.log(getDate("2012-05-09"));
</script>

猜你喜欢

转载自blog.csdn.net/Humor_L/article/details/129049056