代码示例:
<html> <head> <title>Date字符串转化示例</title> </head> <body> <script> var nowDate = new Date("2012-01-01 11:01:01"); window.onload=function(){ alert(nowDate.getTime()); } </script> </body> </html>
笔者在web端、android各浏览器测试均无问题但是ios内置的safari浏览器无法识别包含-的日期字符串,返回NAN或null
解决方法
可通过正则表达式把“-”符号转化为“/”符号
代码示例如下
<html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/> <title>Date字符串转化示例</title> </head> <body> <script> var nowDate = "2012-01-01 11:01:01"; nowDate = nowDate.replace(/-/g,"/"); window.onload=function(){ alert(new Date(nowDate).getTime()); } </script> </body> </html>