先声明一下,没有什么技术含量,但是耗了很长时间来写这个脚本。
时间加add及时间差diff部分与sql server(2005)中的dateadd()和datediff()相吻合,自己验证没有发现不同,可能有没有验证的情况,请大家改良吧Date.prototype.firstdate = function(){
var yyyy = this.getFullYear();
var mtd = this.getMonth();
return new Date(yyyy,mtd,1,0,0,0);
}
Date.prototype.lastdate = function(){
var yyyy = this.getFullYear();
var mtd = this.getMonth() + 1;
var last = new Date(yyyy,mtd,1,0,0,0);
return last.add("dd",-1);
}
Date.prototype.reset = function(part){
var datecopy = new Date(Date.parse(this));
switch(part){
case "ss":
datecopy.setSeconds(0);
break;
case "mi":
datecopy.setMinutes(0);
break;
case "hh":
datecopy.setHours(0);
break;
case "dd":
datecopy.setDate(1);
break;
case "mm":
datecopy.setMonth(0);
break;
}
return datecopy;
}
Date.prototype.add = function(part,num){
var datecopy;
var ms = this.getTime();
num = parseInt(num);
switch(part){
case "ms":
ms +=  num;
break;
case "ss":
ms +=  1000 * num;
break;
case "mi":
ms +=  60 * 1000 * num;
break;
case "hh":
ms +=  60 * 60 * 1000 * num;
break;
case "dd":
ms +=  24 * 60 * 60 * 1000 * num;
break;
case "wk":
ms +=  7 * 24 * 60 * 60 * 1000 * num;
break;
case "mm":
datecopy = new Date(Date.parse(this));
datecopy.setFullYear(this.getFullYear() + Math.floor((this.getMonth() + num) / 12));
var mth = (this.getMonth() + num) % 12;
if(mth < 0)mth +=  12;
datecopy.setMonth(mth);
break;
case "qq":
datecopy = new Date(Date.parse(this));
datecopy.setFullYear(this.getFullYear() + Math.floor((this.getMonth() + 3 * num) / 12));
var mth = (this.getMonth() + 3 * num) % 12;
if(mth < 0)mth +=  12;
datecopy.setMonth(mth);
break;
case "yy":
datecopy = new Date(Date.parse(this));
datecopy.setFullYear(this.getFullYear() + num);
break;
}
if(datecopy == null)
return new Date(ms);
else
return datecopy;
}
Date.prototype.diff = function(part,redate){
var startdate,enddate,diff,tmp;
var re = new RegExp(/^(\d{4})-(0?[1-9]|1[0-2])-(0?[1-9]|[1-2]\d|3[0-1])( (0?\d|1\d|2[0-3]):(0?\d|[1-5]\d):(0?\d|[1-5]\d)([.](\d{1,3}))?)?$/);
var reprefix0 = new RegExp(/^0(\d)/);
startdate = new Date(Date.parse(this));
if(re.test(redate))
{
re.exec(redate);
var yyyy = RegExp.$1;
var mm = RegExp.$2;
var dd = RegExp.$3;
var hh = RegExp.$5;
var mi = RegExp.$6;
var ss = RegExp.$7;
var ms = RegExp.$9;
var yyyy = parseInt(yyyy);
var mm = parseInt(delPrefixZero(mm)) - 1;
dd = parseInt(delPrefixZero(dd));
hh = parseInt(delPrefixZero(hh));
mi = parseInt(delPrefixZero(mi));
ss = parseInt(delPrefixZero(ss));
ms = parseInt(delPrefixZero(ms));
enddate = new Date(yyyy,mm,dd,hh,mi,ss);
}
else
{
try
{
   enddate = new Date(Date.parse(redate));
}
  catch(e)
{
alert(e);
return;
}
}
if(enddate == "Invalid Date"){
alert("非法日期!");
return;
}else{
switch(part){
case "ss":
diff = (enddate - this) / 1000;
break;
case "mi":
diff = enddate.getMinutes() - this.getMinutes();
startdate = startdate.reset("mi");
startdate = startdate.reset("ss");
enddate = enddate.reset("mi");
enddate = enddate.reset("ss");
diff += (enddate-startdate) / 1000 / 60;
break;
case "hh":
diff = enddate.getHours() - this.getHours();
startdate = startdate.reset("hh");
startdate = startdate.reset("mi");
startdate = startdate.reset("ss");
enddate = enddate.reset("hh");
enddate = enddate.reset("mi");
enddate = enddate.reset("ss");
diff += (enddate-startdate) / 1000 / 60 / 60;
break;
case "dd":
diff = enddate.getDate() - this.getDate();
startdate = startdate.reset("dd");
startdate = startdate.reset("hh");
startdate = startdate.reset("mi");
startdate = startdate.reset("ss");
enddate = enddate.reset("dd");
enddate = enddate.reset("hh");
enddate = enddate.reset("mi");
enddate = enddate.reset("ss");
diff += (enddate-startdate) / 1000 / 60 / 60 / 24;
break;
case "wk":
tmp = enddate.getDay() - this.getDay() < 0 ? 1 : 0;
diff = this.diff("dd",enddate) / 7;
if(diff >= 0)diff += tmp;
break;
case "mm":
diff = (enddate.getFullYear() - this.getFullYear()) * 12 + enddate.getMonth() - this.getMonth();
break;
case "qq":
diff = this.diff("mm",enddate) / 3;
diff = Math.floor(diff);
break;
case "yy":
diff = enddate.getFullYear() - this.getFullYear();
break;
default:
diff = (enddate - this);
}
diff = parseInt(diff);
return diff;
}
function delPrefixZero(str)
{
var re = new RegExp(/^0*/);
str = str.replace(re,"");
if(str == "")str = "0";
return str;
}
}

解决方案 »

  1.   

    firstdate:返回当月的第一天
    lastdate:返回当月的最后一天
    add:返回加上一个值后的时间
    diff:返回两个时间之间的差
    yy:年
    qq:季
    mm:月
    wk:周
    dd:日
    hh:小时
    mi:分
    ss:秒
      

  2.   

    reset函数用于将时间的某一部分重置为最小值,月(mm)、日(dd)、时(hh)、分(mi)、秒(ss)
      

  3.   

    验证add
    <script type="text/javascript" src="Date.js"></script>
    <script type="text/javascript">
    var part=["ms","ss","mi","hh","dd","wk","mm","qq","yy"];
    var diff=[-213,213];
    var date=new Date(2009,9,10,9,18,50);
    document.writeln("date="+date.toLocaleString());
    for(var i=0;i<part.length;i++){
    for(var j=0;j<diff.length;j++){
    var date2=date.add(part[i],diff[j]).toLocaleString();
    document.writeln("<br />add("+part[i]+","+diff[j]+")="+date2);
    }
    }
    </script>输出结果
    date=2009年10月10日 9:18:50
    add(ms,-213) =2009年10月10日 9:18:49
    add(ms,213) =2009年10月10日 9:18:50
    add(ss,-213) =2009年10月10日 9:15:17
    add(ss,213) =2009年10月10日 9:22:23
    add(mi,-213) =2009年10月10日 5:45:50
    add(mi,213) =2009年10月10日 12:51:50
    add(hh,-213) =2009年10月1日 12:18:50
    add(hh,213) =2009年10月19日 6:18:50
    add(dd,-213) =2009年3月11日 9:18:50
    add(dd,213) =2010年5月11日 9:18:50
    add(wk,-213) =2005年9月10日 9:18:50
    add(wk,213) =2013年11月9日 9:18:50
    add(mm,-213) =1992年1月10日 9:18:50
    add(mm,213) =2027年7月10日 9:18:50
    add(qq,-213) =1956年7月10日 9:18:50
    add(qq,213) =2063年1月10日 9:18:50
    add(yy,-213) =1796年10月10日 9:18:50
    add(yy,213) =2222年10月10日 9:18:50转为SQL
    declare @dd datetime
    set @dd='2009-10-10 9:18:50'
    create table #testtable(sqldate datetime,jsdate varchar(50))
    insert #testtable values(dateadd(ms,-213,@dd),'2009-10-10 9:18:49')
    insert #testtable values(dateadd(ms,213,@dd),'2009-10-10 9:18:50')
    insert #testtable values(dateadd(ss,-213,@dd),'2009-10-10 9:15:17')
    insert #testtable values(dateadd(ss,213,@dd),'2009-10-10 9:22:23')
    insert #testtable values(dateadd(mi,-213,@dd),'2009-10-10 5:45:50')
    insert #testtable values(dateadd(mi,213,@dd),'2009-10-10 12:51:50')
    insert #testtable values(dateadd(hh,-213,@dd),'2009-10-1 12:18:50')
    insert #testtable values(dateadd(hh,213,@dd),'2009-10-19 6:18:50')
    insert #testtable values(dateadd(dd,-213,@dd),'2009-3-11 9:18:50')
    insert #testtable values(dateadd(dd,213,@dd),'2010-5-11 9:18:50')
    insert #testtable values(dateadd(wk,-213,@dd),'2005-9-10 9:18:50')
    insert #testtable values(dateadd(wk,213,@dd),'2013-11-9 9:18:50')
    insert #testtable values(dateadd(mm,-213,@dd),'1992-1-10 9:18:50')
    insert #testtable values(dateadd(mm,213,@dd),'2027-7-10 9:18:50')
    insert #testtable values(dateadd(qq,-213,@dd),'1956-7-10 9:18:50')
    insert #testtable values(dateadd(qq,213,@dd),'2063-1-10 9:18:50')
    insert #testtable values(dateadd(yy,-213,@dd),'1796-10-10 9:18:50')
    insert #testtable values(dateadd(yy,213,@dd),'2222-10-10 9:18:50')
    select * from #testtable
    drop table #testtableSQL输出结果
    sqldate jsdate
    2009-10-10 09:18:49.787 2009-10-10 9:18:49
    2009-10-10 09:18:50.213 2009-10-10 9:18:50
    2009-10-10 09:15:17.000 2009-10-10 9:15:17
    2009-10-10 09:22:23.000 2009-10-10 9:22:23
    2009-10-10 05:45:50.000 2009-10-10 5:45:50
    2009-10-10 12:51:50.000 2009-10-10 12:51:50
    2009-10-01 12:18:50.000 2009-10-1 12:18:50
    2009-10-19 06:18:50.000 2009-10-19 6:18:50
    2009-03-11 09:18:50.000 2009-3-11 9:18:50
    2010-05-11 09:18:50.000 2010-5-11 9:18:50
    2005-09-10 09:18:50.000 2005-9-10 9:18:50
    2013-11-09 09:18:50.000 2013-11-9 9:18:50
    1992-01-10 09:18:50.000 1992-1-10 9:18:50
    2027-07-10 09:18:50.000 2027-7-10 9:18:50
    1956-07-10 09:18:50.000 1956-7-10 9:18:50
    2063-01-10 09:18:50.000 2063-1-10 9:18:50
    1796-10-10 09:18:50.000 1796-10-10 9:18:50
    2222-10-10 09:18:50.000 2222-10-10 9:18:50
      

  4.   

    diff验证
    <script type="text/javascript">
    var part=["ms","ss","mi","hh","dd","wk","mm","qq","yy"];
    var diff=[-3,50];
    var date=new Date(2009,9,10,9,18,50);
    var str1=date.toLocaleString().replace("年","-").replace("月","-").replace("日","");
    for(var i=0;i<part.length;i++){
    for(var j=0;j<diff.length;j++){
    var date2=date.add(part[i],diff[j]);
    var str2=date2.toLocaleString().replace("年","-").replace("月","-").replace("日","");
    for(var k=2;k<part.length;k++){
    var span=date.diff(part[k],date2);
    document.writeln("<br />insert #testtable values('"+str1+"','"+str2+"','"+part[k]+"',"+span+",datediff("+part[k]+",'"+str1+"','"+str2+"'))");
    }
    }
    }
    </script>
      

  5.   

    //SQL语句(部分省略)create table #testtable(date1 datetime,date2 datetime,part varchar(10),jsspan int,sqlspan int)
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','mi',0,datediff(mi,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','hh',0,datediff(hh,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:49','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 9:18:49'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','mi',0,datediff(mi,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','hh',0,datediff(hh,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:50','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 9:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','mi',0,datediff(mi,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','hh',0,datediff(hh,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:18:47','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 9:18:47'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','mi',1,datediff(mi,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','hh',0,datediff(hh,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:19:40','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 9:19:40'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','mi',-3,datediff(mi,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','hh',0,datediff(hh,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 9:15:50','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 9:15:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','mi',50,datediff(mi,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','hh',1,datediff(hh,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 10:08:50','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 10:08:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','mi',-180,datediff(mi,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','hh',-3,datediff(hh,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','dd',0,datediff(dd,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','wk',0,datediff(wk,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','mm',0,datediff(mm,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','qq',0,datediff(qq,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    insert #testtable values('2009-10-10 9:18:50','2009-10-10 6:18:50','yy',0,datediff(yy,'2009-10-10 9:18:50','2009-10-10 6:18:50'))
    --回复内容过长!,省略部分
    select * from #testtable where jsspan<>sqlspan
    drop table #testtable//SQL执行结果
    结果集为空
      

  6.   

    reset方法  “var datecopy = new Date(Date.parse(this));”
    这么写不太明白,跟var datecopy=this;这么写有什么分别
      

  7.   


    前者是新建一个Date对象,后者引用原对象。修改前者不会影响原对象。