日期:2014-05-16 浏览次数:20438 次
void Main() { DateTime dt1= DateTime.Parse("2012-01-01"); DateTime dt2= DateTime.Parse("2012-01-25"); TimeSpan ts=dt2-dt1; int x=0; int y=0; for(int i=0;i<(int)ts.TotalDays;i++) { if(dt1.DayOfWeek==DayOfWeek.Tuesday) x++; if(dt1.DayOfWeek==DayOfWeek.Thursday) y++; dt1=dt1.AddDays(1); } Console.WriteLine("Tuesday:{0} \tThursday{1}",x,y); //Tuesday:4 Thursday3 }
------解决方案--------------------
Date.prototype.increment=function(){ return new Date(this.getTime()+3600*24*1000); } //第三个参数就是对应的星期几(0表表示星期日,1表示星期一。。。。) function getNum(a,b,c){ var a=new Date(Date.parse(a)); var b= new Date(Date.parse(b)); var num = 0; while(a<=b){ if(c==a.getDay()){ num++; } a=a.increment(); } return num; } //直接调用 alert(getNum("2012-01-01","2012-01-25",1));
------解决方案--------------------
var sd1 = new Date(2012,0,3);//开始日期 var ed1 = new Date(2012,0,24);//结束日期,月份要减一 calwd = function(sd,ed,cw){//第三个参数:0-星期日,1-星期一, var daydiff = (ed.getTime()-sd.getTime())/86400000+1; var daystart = sd.getDay(); return Math.ceil((daydiff - ((cw - daystart)>=0 ? cw - daystart : cw - daystart + 7))/7); } alert(calwd(sd1,ed1,2));