下面的函数根据给定的参数计算年龄。
var now = new Date();
var past = new Date(select_year + "-" + select_month + "-" + select_day);
var nowYear = now.getFullYear();
var pastYear = past.getFullYear();
var age = nowYear - pastYear;
它在Chrome中完美运行。
但是当我切换到Safari时,以下行显示Invalid Date
:
var past = new Date(select_year + "-" + select_month + "-" + select_day);
我假设它未能重建日期。
有没有办法解决这个问题?
答案 0 :(得分:2)
对于Internet Explorer和Safari,您需要稍微更改格式。我相信这对你有用:
var d = new Date(2011, 01, 07);
var d = new Date(2011, 01, 07, 11, 05, 00);
var d = new Date("02/07/2011");
var d = new Date("02/07/2011 11:05:00");
var d = new Date(1297076700000);
var d = new Date("Mon Feb 07 2011 11:05:00 GMT");
找到here。