【问题标题】:How to count days quantity [duplicate]如何计算天数[重复]
【发布时间】:2011-10-11 09:10:12
【问题描述】:

我已签入年、月、日并签出年、月、日。我无法解决问题,我如何计算这个范围内的天数

【问题讨论】:

    标签: c# datetime time timespan


    【解决方案1】:
    var d1 = new DateTime(year1, month1, day1);
    var d2 = new DateTime(year2, month2, day2);
    TimeSpan t = d2 - d1;
    var elapsedDays = t.Days;
    

    【讨论】:

      【解决方案2】:

      试试这个:

      TimeSpan difference = endTime.Subtract(startTime); 
      int numDays = difference.Days;
      

      【讨论】:

        【解决方案3】:

        从另一个中减去DateTime(或DateTimeOffset)将得到TimeSpanTimeSpan 结构有一个 TotalDays 属性,它应该给你你正在寻找的东西。

        Here'sTimeSpan 的 MSDN 页面链接。

        【讨论】:

          【解决方案4】:
          (new DateTime(endYear, endMonth, endDay) - new DateTime(startYear, startMonth, startDay)).TotalDays
          

          【讨论】:

            【解决方案5】:
            DateTime checkin //set to checkin date
            DateTime checkout //set to checkout date
            TimeSpan ts = checkout.Subtract(checkin);
            int dayDifference = ts.TotalDays; //this is your days
            

            【讨论】:

            • .Days 在总时间跨度为 34 天的情况下将为您提供 3。
            猜你喜欢
            • 2012-09-10
            • 2013-06-26
            • 2013-10-08
            • 1970-01-01
            • 1970-01-01
            • 2016-10-11
            • 1970-01-01
            • 2014-05-08
            相关资源
            最近更新 更多