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

Recommended Answers

All 3 Replies

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

@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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.