954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

date time (asp.net)

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

vinaysrk919
Newbie Poster
16 posts since Aug 2011
Reputation Points: 10
Solved Threads: 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.

LaxLoafer
Junior Poster in Training
68 posts since Aug 2011
Reputation Points: 43
Solved Threads: 12
 

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

vinaysrk919
Newbie Poster
16 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You