求两日期之间的天数

  // 求两日期之间的天数
  public static int DateDiff(DateTime beginDate, DateTime endDate)
  {
   TimeSpan diff = endDate.Date - beginDate.Date;
   return diff.Days ;
  //以下是另一种算法;
   if(beginDate.Date <= endDate.Date)
   {
    return GetDateTimes(beginDate,endDate).Length;
   }
   else
   {
    return -GetDateTimes(beginDate,endDate).Length;
   }    
 //以下是另一种算法;
 // FromOADate()方法的日期零点值为1899年12月30日0点0分0秒0毫秒,顺逆推为加减一天  

     DateTime littleDate = beginDate;
   DateTime bigDate = endDate;
   bool changed = false;
   if (littleDate > bigDate)
   {
    DateTime md = littleDate;
    littleDate = bigDate;
    bigDate = md;
    changed = true;
   }
   if(littleDate < Convert.ToDateTime("1899-12-30") )
   {
    return -1; //只比较1899-12-30之后的日期
   }
   string lstring = littleDate.ToString("d");
   string bstring = bigDate.ToString("d");
   string strCurDate,strOthDate;   
   double i=0, j=0;  //可以不从0开始,以减少循环次数 
   do
   {   
    i ;   
    strCurDate=DateTime.FromOADate(i).ToString("d");   
   }   
   while(strCurDate != lstring );   

   strOthDate = strCurDate;
   while(strOthDate != bstring)
   {   
    j ;
    i ;
    strOthDate=DateTime.FromOADate(i).ToString("d");   
   }   

   if (changed == true)
   {
    j = -j;
   }
   
     return Convert.ToInt32(j);
 
  }
 上面列出三种算法.第二种算法还需要GetDateTimes函数的支持,这个函数是返回一个由两日期之间的日期组成的数组。如果用哪种算法可以把另外两种算法注掉。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值