i have the coding as below given...
and i getting error

TimeSpan date1 = TimeSpan.Parse(TextBox1.Text);
TimeSpan date2 = TimeSpan.Parse(TextBox2.Text);
TimeSpan ts = date1 - date2;
Textbox3.Text=ts;

Textbox1 show the current date,
Textbox2 show the previous date,
and
Textbox3 show the difference of textbox1 and textbox2 as the above given code..

i'm gettin an error (input string not correct format)
any solution .....for the above

Recommended Answers

All 2 Replies

Hi Vinaysrk

The TimeSpan.Parse() method accepts strings representing timespans, not dates. The format for timespans is documented here on MSDN:

TimeSpan.Parse Method (String)

I notice also in line 4 of your code you're attempting to assign a TimeSpan to a string. You'll get a "Cannot implicitly convert type" error message at compile time.

Assuming date1 and date2 actually contain dates, I think your code should use the DateTime structure and look something like this:

DateTime date1 = DateTime.Parse(TextBox1.Text);
DateTime date2 = DateTime.Parse(TextBox2.Text);
TimeSpan ts = date1 - date2;
TextBox3.Text = ts.ToString();

DateTime.Parse will thow exceptions if an invalid date is entered, so you'll want to catch this, unless you're 100% sure the user is never going to enter an invalid date.

Hi Vinaysrk

The TimeSpan.Parse() method accepts strings representing timespans, not dates. The format for timespans is documented here on MSDN:

TimeSpan.Parse Method (String)

I notice also in line 4 of your code you're attempting to assign a TimeSpan to a string. You'll get a "Cannot implicitly convert type" error message at compile time.

Assuming date1 and date2 actually contain dates, I think your code should use the DateTime structure and look something like this:

DateTime date1 = DateTime.Parse(TextBox1.Text);
DateTime date2 = DateTime.Parse(TextBox2.Text);
TimeSpan ts = date1 - date2;
TextBox3.Text = ts.ToString();

DateTime.Parse will thow exceptions if an invalid date is entered, so you'll want to catch this, unless you're 100% sure the user is never going to enter an invalid date.

Thanks for the reply....!

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.