在C#中,我试图显示两个日期时间选择器之间的天数,包括所选的天数,但如果选择今天的日期,则不会添加1天

在C#中,我试图显示两个日期时间选择器之间的天数,包括所选的天数,但如果选择今天的日期,则不会添加1天,c#,date,time,timespan,picker,C#,Date,Time,Timespan,Picker,如果我从今天开始计算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 d

如果我从今天开始计算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();
}

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

检查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

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

检查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

更改了数学运算符并将其转换为整数,如下所示,现在正在工作

    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();
    }

更改了数学运算符并将其转换为整数,如下所示,现在正在工作

    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();
    }

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