0

Hello Everybody,

I'm stuck with my little project. I want to select multiple days in a monthcalendar and count the days and display this in a textbox.

I looked at different sites, but keep getting the error

When converting a string to DateTime, parse the string to take the date before putting each variable into DateTime object. Even with the explanation I.m not getting it to work

    private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
        {

            String DateTime1 = monthCalendar1.SelectionRange.Start.ToShortDateString();
            String DateTime2 = monthCalendar1.SelectionEnd.ToShortDateString();


            DateTime d1 = DateTime.ParseExact(DateTime1, "dd/MM/yyyy", null);
            DateTime d2 = DateTime.ParseExact(DateTime2, "dd/MM/yyyy", null);
            TimeSpan t1 = d2.Subtract(d1);

            textBox1.Text = t1.ToString();

            }

thanks for the help

2
Contributors
3
Replies
12
Views
2 Years
Discussion Span
Last Post by ddanbe
0

SelectionStart and SelectionEnd of the monthcalendar are datetime structures.
So the following should work:
TimeSpan t1 = monthCalendar1.SelectionEnd.Subtract(monthCalendar1.SelectionStart);
Success!

0

@ddanbe
Thanks

The result wich i get when I select four days (for example) = 3:23:59:59

Can I convert this to 4?

using math.round? or do i have to change someting else?

Thanks

0

How did you arrive at that figure?
If you use the Days property of the timespan it should give you exaxtly 4.
Perhaps read some more about the Timespan structure in this MSDN article.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.