日和月计算不会返回 C# 中的预期输出

本文关键字:输出 返回 计算 | 更新日期: 2023-09-27 18:35:50

我想计算拖曳日之间的时差。

这是我的代码:

private static string CalculateDayMonth(DateTime date1, DateTime date2)
{
    int totalMonth = 0;
    string returnValue = "";
    if (date1 >= date2)
    {
        int days = date1.Day - date2.Day;
        int months = date1.Month - date2.Month;
        int years = date1.Year - date2.Year;
        if (months < 0)
        {
            months += 12;
            years -= 1;
        }
        if (days < 0)
        {
            int dayToSubtract = DateTime.DaysInMonth(date2.Year, date2.Month);
            days = days + dayToSubtract;
            months -= 1;
        }
        if (years > 0)
            totalMonth = years * 12;
        else
            years = 0;
        totalMonth += months;
        if (totalMonth <= 0)
            returnValue = string.Format(AppResources.Label50414, days.ToString());
        else
            returnValue = string.Format(AppResources.Label50415, totalMonth.ToString());
    }
    return returnValue;
}

我的输入日期是:2015-1-2下午14:02:47 -2014-12-15下午16:14:50

预计输出为 17 天

但实际输出是:18天

我也尝试其他一些例子。但这也给出了相同的输出。

请让我知道我哪里做错了。

日和月计算不会返回 C# 中的预期输出

您的代码甚至不会计算您的小时、分钟和秒部分。如果没有小时部分,1 月 2 日至 12 月 15 日之间有 18 天的午夜。

但是您的链接也可以计算小时,分钟和秒部分。由于 12 月 15 日的小时部分高于 1 月 2 日,因此这些日期之间的差异将小于 18 天。这就是为什么它们不同。

看起来你只需要使用简单的TimeSpan;

var dt1 = new DateTime(2015, 1, 2, 14, 2, 47);
var dt2 = new DateTime(2014, 12, 15, 16, 14, 50);
var ts = dt1 - dt2;
Console.WriteLine(ts.Days); // 17
Console.WriteLine(ts.Hours); // 21
Console.WriteLine(ts.Minutes); // 47
Console.WriteLine(ts.Seconds); // 57

按格式;

Console.WriteLine("{0} days, {1} hours, {2} minutes and {3} seconds",
                  ts.Days,
                  ts.Hours,
                  ts.Minutes,
                  ts.Seconds); // 17 days, 21 hours, 47 minutes and 57 seconds
     private struct DateResult
    {
        public int years;
        public int months;
        public int days;
    }
    private DateResult Date_Difference(DateTime d1, DateTime d2)
    {
        DateResult d_r;
        d_r.years = 0;
        d_r.months = 0;
        d_r.days = 0;
        DateTime frm_date, to_Date;
        int key=0;
        if (d1 > d2)
        {
            frm_date = d2;
            to_Date = d1;
        }
        else
        {
            frm_date = d1;
            to_Date = d2;
        }
        if (frm_date.Day > to_Date.Day) //10  5  //7  24
        {
            key = month_day[frm_date.Month - 1];
            if (key == -1)
            {
                if (DateTime.IsLeapYear(frm_date.Year))
                    key = 29;
                else
                    key = 28;
            }                
        }
        if (key == 0)
        {
            d_r.days = to_Date.Day - frm_date.Day;
        }
        else
        {
            d_r.days = (to_Date.Day + key) - frm_date.Day;
            key = 1;
        }
        if ((frm_date.Month+key) > to_Date.Month)
        {
            d_r.months = (to_Date.Month + 12) - (frm_date.Month + key);
            key = 1;
        }
        else
        {
            d_r.months = to_Date.Month - (frm_date.Month+key);
            key=0;
        }
        d_r.years = to_Date.Year - (frm_date.Year+key);
        return d_r; 
    }

这段代码在 C# 中。它对我有用...我们还可以了解小时,分钟和秒