【问题标题】:In C# I am trying to show number of days between 2 datetime pickers, inclusive of days selected but it does not add 1 day if I select todays date在 C# 中,我试图显示 2 个日期时间选择器之间的天数,包括选择的天数,但如果我选择今天的日期,它不会添加 1 天
【发布时间】:2019-10-04 11:15:28
【问题描述】:

如果我从今天开始计算 5 天,则显示为 4 天。如果我将开始日期移回 1,它会显示 6
这是我目前拥有和工作的内容,除非我使用今天的日期作为开始日期。

private void DaysToShow() 
{
    //Find the difference in the days selected in the drop down menu so we can calculate
    DateTime dtDateOnQuay = dtpDateOnQuay.Value;
    DateTime dtDateLeft = dtpDateLeft.Value;
    TimeSpan difference = dtDateLeft - dtDateOnQuay;

    //As the days are inclusive and the above gets the days in between, add 1
    m_iDaysRent = difference.Days + 1;
    m_iDaysDetention = m_iDaysRent;

    if (dtpDateReturned.Checked)
    {
        TimeSpan oDetentionDiff = dtpDateReturned.Value - dtpDateOnQuay.Value;
        m_iDaysDetention = oDetentionDiff.Days + 1;
    }

    txtDaysOnQuay.Text = m_iDaysRent.ToString();
    txtDaysDetention.Text = m_iDaysDetention.ToString();
}

【问题讨论】:

  • 请编辑您的问题并在标签中指定您使用的编程语言

标签: c# date time timespan picker


【解决方案1】:

我认为您的问题可能是日期的时间角度。您不需要它,因此您可能应该尝试以下解决方案。

检查忽略时间的 DaysToShowByDateComp。

public class Program
{
    public static void Main()
    {
        DateTime endDate = DateTime.Parse("2019-10-01 23:59:59");
        DateTime startDate2 = DateTime.Parse("2019-10-04 00:00:00");
        DateTime endDate2 = DateTime.Parse("2019-10-01 00:00:00");

        Console.WriteLine(DateTime.Now.ToString());
        Console.WriteLine(endDate.ToString());

        Console.WriteLine("With today timespan:" + DaysToShowByTimeComp(DateTime.Now, endDate));        
        Console.WriteLine("With today date comparison:" + DaysToShowByDateComp(DateTime.Now, endDate));

        Console.WriteLine("With other timespan:" + DaysToShowByTimeComp(startDate2, endDate2));     
        Console.WriteLine("With other date comparison:" + DaysToShowByDateComp(startDate2, endDate2));
    }

    private static int DaysToShowByTimeComp(DateTime start, DateTime end) 
    {
        //Find the difference in the days selected in the drop down menu so we can calculate
        DateTime dtDateOnQuay = end;
        DateTime dtDateLeft = start;
        TimeSpan difference = dtDateLeft - dtDateOnQuay;

        //As the days are inclusive and the above gets the days in between, add 1
        return difference.Days + 1;
    }

    private static int DaysToShowByDateComp(DateTime start, DateTime end) 
    {
        return (int)((start.Date - end.Date).TotalDays) + 1;
    }
}

输出将是

10/4/2019 2:18:23 PM 
10/1/2019 11:59:59 PM 
With today timespan: 3 
With today date comparison: 4 
With other timespan: 4
With other date comparison: 4

【讨论】:

    【解决方案2】:

    更改了数学运算符并转换为 int,如下所示,现在可以使用

        private void DaysToShow() 
            {
            //Find the difference in the days selected in the drop down menu so we can         calculate
            DateTime dtDateOnQuay = dtpDateOnQuay.Value;
            DateTime dtDateLeft = dtpDateLeft.Value; 
            TimeSpan difference = dtDateLeft.Subtract(dtDateOnQuay);
    
            //As the days are inclusive and the above gets the days in between, add 1
            m_iDaysRent = Convert.ToInt32(difference.TotalDays) +1;  
            m_iDaysDetention = m_iDaysRent;
    
            if (dtpDateReturned.Checked)
            {
                TimeSpan oDetentionDiff = dtpDateReturned.Value - dtpDateOnQuay.Value;
                m_iDaysDetention = oDetentionDiff.Days + 1;
            }
    
            txtDaysOnQuay.Text = m_iDaysRent.ToString();
            txtDaysDetention.Text = m_iDaysDetention.ToString();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-24
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多