<mce:script type="text/javascript"><!--
document.write("<br/>" + new Date("February 3,2009"));
document.write("<br/>" + new Date("February 3,2009 10:52:03"));
document.write("<br/>");
document.write("<br/>" + new Date(2009,1,3));
document.write("<br/>" + new Date(2009,1,3,10,52,03));
document.write("<br/>");
document.write("<br/>" + new Date(Date.parse("February 3,2009")));
document.write("<br/>" + new Date(Date.parse("February 3,2009 10:52:03")));
document.write("<br/>" + new Date(Date.parse(2009,1,3))); //Output: NAN
document.write("<br/>" + new Date(Date.parse(2009,1,3,10,52,03))); //Output: NAN
document.write("<br/>" + new Date(Date.parse("2009/02/03")));
document.write("<br/>");
document.write("<br/>" + new Date("2009/02/03"));
document.write("<br/>" + new Date("2009/02/03 11:12:13"));
document.write("<br/>" + new Date("2009-02-03")); //Output: NAN
// --></mce:script>
输出结果:
Tue Feb 3 00:00:00 UTC+0800 2009
Tue Feb 3 10:52:03 UTC+0800 2009
Tue Feb 3 00:00:00 UTC+0800 2009
Tue Feb 3 10:52:03 UTC+0800 2009
Tue Feb 3 00:00:00 UTC+0800 2009
Tue Feb 3 10:52:03 UTC+0800 2009
NaN
NaN
Tue Feb 3 00:00:00 UTC+0800 2009
Tue Feb 3 00:00:00 UTC+0800 2009
Tue Feb 3 11:12:13 UTC+0800 2009
NaN
-------------------
window.onload=function(){
var dependedVal="2005-3-4";
//根据日期字符串转换成日期
var regEx = new RegExp("\\-","gi");
dependedVal=dependedVal.replace(regEx,"/");
//dependedVal=dependedVal.replace("\\-","/");//这样不行
alert(dependedVal)
//parse 需要 2005/3/4 这种格式
var milliseconds=Date.parse(dependedVal);
alert(milliseconds)
var dependedDate=new Date();
dependedDate.setTime(milliseconds);
var now = new Date();
//注意括号,优先级问题,无奈
alert("相隔年数:"+(now.getFullYear() - dependedDate.getFullYear()));
}