【问题标题】:DateTime difference in days on the basis of Date onlyDateTime 仅基于 Date 的天数差异
【发布时间】:2012-10-23 06:11:56
【问题描述】:

我需要找出两个日期之间的天数差。

例如:

输入:**startDate** = 12-31-2012 23hr:59mn:00sec, **endDate** = 01-01-2013 00hr:15mn:00sec

预期输出:1

我尝试了以下方法:

  1. (dt1-dt2).TotalDays 并转换为整数,但没有给我适当的答案,因为必须将 double 转换为 int - 尝试过 Math.Ceiling、Convert.To...
  2. dt1.day - dt2.day 不能跨月工作
  3. dt.Substract() 的输出与上述选项 1 相同。

以上都不起作用,所以我最终编写了以下代码。代码运行良好,但我觉得必须有一个只有几行代码的解决方案。

public static int GetDifferenceInDaysX(this DateTime startDate, DateTime endDate)
    {
        //Initializing with 0 as default return value
        int difference = 0;

        //If either of the dates are not set then return 0 instead of throwing an exception
        if (startDate == default(DateTime) | endDate == default(DateTime))
            return difference;

        //If the dates are same then return 0
        if (startDate.ToShortDateString() == endDate.ToShortDateString())
            return difference;

        //startDate moving towards endDate either with increment or decrement
        while (startDate.AddDays(difference).ToShortDateString() != endDate.ToShortDateString())
        {
            difference = (startDate < endDate) ? ++difference : --difference;
        }

        return difference;
    }

注意:我在 while 循环迭代中没有任何性能问题,因为最大差异不会超过 30 到 45 天。

【问题讨论】:

    标签: c# .net c#-4.0


    【解决方案1】:

    嗯,听起来你想要天数的差异,忽略时间部分。 DateTime 的时间组件重置为 00:00:00 是 Date 属性为您提供的:

    (startDate.Date - endDate.Date).TotalDays
    

    【讨论】:

    • 简单直接。只是想知道我怎么想不起来... :((...感谢 Damien 提供了一段甜蜜的代码... :))
    【解决方案2】:

    如果您使用DateTime.Date 属性,这将消除时间

    date1.Date.Subtract(date2.Date).Days
    

    【讨论】:

    • 当然,Amiram,它有效.. 但已将较早的帖子标记为已接受的答案。感谢您的参与。
    【解决方案3】:

    使用TimeStamp。只需减去两个日期(使用DateTime.Date 属性),得到时间跨度的差异并返回TotalDays

    TimeSpan ts = endDate.Date - startDate.Date;
    double TotalDays = ts.TotalDays;
    

    所以你的扩展方法可以很简单:

    public static int GetDifferenceInDaysX(this DateTime startDate, DateTime endDate)
        {
          return (int) (endDate.Date - startDate.Date).TotalDays;
          // to return just a int part of the Total days, you may round it according to your requirement
        }
    

    编辑:由于问题已被编辑,您可以查看以下示例。 考虑以下两个日期。

    DateTime startDate = new DateTime(2012, 12, 31, 23, 59, 00);
    DateTime endDate = new DateTime(2013, 01, 01, 00, 15, 00); 
    

    扩展方法可以写成:

    public static int GetDifferenceInDaysX(this DateTime startDate, DateTime endDate)
        {
            TimeSpan ts = endDate - startDate;
            int totalDays = (int) Math.Ceiling(ts.TotalDays);
            if (ts.TotalDays < 1 && ts.TotalDays > 0)
                totalDays = 1;
            else
                totalDays = (int) (ts.TotalDays);
            return totalDays;
        }
    

    对于上述日期,它会给你 1

    【讨论】:

    • Habib,我觉得 Damien_The_Unbeliever 的做法更优雅。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2018-07-30
    相关资源
    最近更新 更多