window.οnlοad=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())); }