0

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

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by vinaysrk919
0

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.

0

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....!

This question has already been answered. 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.