以下函数可以计算两个 Date 之间的差值(年,月,日,小时,分钟,秒)
Date.prototype.diff =function(d,f){
switch (f.toLowerCase()){
case "y": return this.getFullYear()-d.getFullYear();
case "m": return (this.getFullYear()-d.getFullYear())*12 + (this.getMonth() - d.getMonth());
case "d": return Math.ceil((this.getTime()-d.getTime())/(1000*60*60*24));
case "h": return (this.getTime()-d.getTime())/(1000*60*60);
case "mi": return (this.getTime()-d.getTime())/(1000*60);
case "s": return (this.getTime()-d.getTime())/1000;
}
}例如:var now =new Date();
     var ago =new Date("2003","8","10");
     alert(now.diff(ago,"d")); //将显示当前日期和"2003-8-10"相差的天数剩下的你就应该没问题了。