多日期差异// C#

时间:2011-11-11 09:30:25

标签: c# .net c#-4.0

如何计算多日期差异,如

startDate1=("dd-MM-yy") 20-08-2011 
endDate1=25-08-11 

另一个

startdate2=27-08-2011 
endDate2=30-08-2011 

这样输出

(endDate1-startDate1)+(endDate2-StartDate2) == 8days //only in terms of days

6 个答案:

答案 0 :(得分:2)

使用TimeSpan计算日期差异。 例如:

TimeSpan ts = Date1 - Date2; 
int numberOfDays = ts.Days; 

可以找到更多信息here

答案 1 :(得分:2)

日期 - 或+日期将返回TimeSpan,它有一个名为“Days”的属性是您需要的。

((endDate1-startDate)+(endDate2-StartDate2)).Days

答案 2 :(得分:0)

试试这段代码: -

            DateTime d1 = StarDate
            DateTime d2 = EndDate;
            TimeSpan t1 = d2.Subtract(d1);
            days = t1.Days;
            hours = t1.Hours;

答案 3 :(得分:0)

试试这个,

        DateTime strdate = Convert.ToDateTime("1/1/2011");
        DateTime enddate = Convert.ToDateTime("1/10/2011");

        DateTime strdate1 = Convert.ToDateTime("1/1/2011");
        DateTime enddate1 = Convert.ToDateTime("1/10/2011");

        int resultdays = (enddate.Subtract(strdate) + enddate1.Subtract(strdate1)).Days;

答案 4 :(得分:0)

完整的代码/答案....

public class DateController : Controller
{

  public ActionResult date()
    {
        int allDiff;
        List<int> list=new List<int>();
        int flag = 0;
        int conflict = 0;
        List<int> conf = new List<int>();
        conf.Add(0);
        int a = 0;

        DateTime[] startDate = new DateTime[3];
        startDate[0] = new DateTime(2011, 11, 5);
        startDate[1] = new DateTime(2011, 11,7);
        startDate[2] = new DateTime(2011, 11, 15);

        DateTime[] endDate = new DateTime[3];
        endDate[0] = new DateTime(2011, 11, 10);
        endDate[1] = new DateTime(2011, 11,12);
        endDate[2] = new DateTime(2011, 11, 20);


        DateTime Min= startDate.Min();
        DateTime Max = endDate.Max();

        TimeSpan span = Max - Min;
        int total = span.Days;
        ViewBag.globalTotal = total;


        foreach (DateTime e in endDate)
        {

            foreach (DateTime s in startDate)
            {
                if (s >= e)
                {
                    TimeSpan span1 = s - e;
                    allDiff = span1.Days;                    
                    list.Add(allDiff);
                    flag = 1;
                    conflict = 1;
                }
                else {
                    flag = 0;
                }

            }
            if((list.Count==1)&&(conflict==1)&&(list!=null)){
                 a = list[0];
                conf.Add(a);
            }


            if ((flag == 1)&&(list.Count>1))
            {
                int m = list.Min();
                ViewBag.dhiraj = m;
                total = total - m;
                list.Clear();
            }

        }
        int confl= conf.Min();
        total=total-confl;
        ViewBag.Total = total;

        return View();
    }

}

`

答案 5 :(得分:0)

如果你想要在SQL中使用确切的DateDiff函数,你可以删除Date变量的时间戳,然后从另一个中减去一个。它会给你确切的天数。 防爆。

DateTime dt = DateTime.Parse(fromDate.ToShortDateString()); 
DateTime dt1 = DateTime.Parse(toDate.ToShortDateString()); 
int noOfDays = dt.Subtract(dt1).TotalDays;