javascript将时间字符串转化为时间的方法:1、根据毫秒数构建date对象,代码为【var date = new date(timestamp)】;2、格式化日期,代码为【datetime =date.tolocalestring】。
本教程操作环境:windows7系统、javascript1.8.5版,dell g3电脑。
javascript将时间字符串转化为时间的方法:
对于时间字符串格式为:2017-03-03 12:23:55;
ie:显示无效的日期
new date("2017-03-3 12:23:55")//[date] invalid date[date] invalid date
chrome和firefox:正确显示
new date("2017-03-3 12:23:55")//fri mar 03 2017 12:23:55 gmt+0800 (中国标准时间)
解决差异:
时间字符串格式统一转化为:"2017/03/03 12:23:55";
var date = '2015-03-05 17:59:00';date = date.substring(0,19); date = date.replace(/-/g,'/'); var timestamp = new date(date).gettime();document.write(timestamp);// 根据毫秒数构建 date 对象var date = new date(timestamp);// 格式化日期datetime = date.tolocalestring();alert(datetime);
相关免费学习推荐:javascript视频教程
以上就是javascript如何将时间字符串转化为时间的详细内容。