Using DateTime and Printing it in textbox

photo123

Member
Joined
Feb 11, 2020
Messages
18
Programming Experience
Beginner
I have this code here that I'm using to print the number of days between two dates. After the user enters the dates in 2 text boxes on my form, I have a third text box that displays the amount of days. For example: 1st text box: 1/2/2020 | 2nd text box: 1/10/2020 | 3rd textbox: 8.00:00:00. In the third box I just want the number of days I don't want it in that time format. I understand that's what TimeSpan does but is there a way to format it so I just get 8 in the text box? I went through the properties for the text box but I couldn't find anything to format it. Is TimeSpan the wrong thing to use in this case?

C#:
private void btnCalculate_Click(object sender, EventArgs e)
{
DateTime arrive = DateTime.Parse(txtArrivalDate.Text);
DateTime depart = DateTime.Parse(txtDepartureDate.Text);
TimeSpan span = depart - arrive;
txtDays.Text = span.ToString();
}
 
Last edited:
Please edit your post, and insert your code with code tags when posting to the forums.

You can not delete threads without a very good reason. And that also requires moderators approval for such reason.

If you've found a solution, how about sharing that solution instead of keeping it all to yourself, and perhaps by contributing back with an answer to your own issue can be considered a good gesture for the amount of times you've already received good answers to your other topics from us? Your answer will likely help a future reader, so it would be appreciated if you shared that solution.
 
I have this code here that I'm using to print the number of days between two dates. After the user enters the dates in 2 text boxes on my form, I have a third text box that displays the amount of days. For example: 1st text box: 1/2/2020 | 2nd text box: 1/10/2020 | 3rd textbox: 8.00:00:00. In the third box I just want the number of days I don't want it in that time format. I understand that's what TimeSpan does but is there a way to format it so I just get 8 in the text box? I went through the properties for the text box but I couldn't find anything to format it. Is TimeSpan the wrong thing to use in this case?

private void btnCalculate_Click(object sender, EventArgs e)
{
DateTime arrive = DateTime.Parse(txtArrivalDate.Text);
DateTime depart = DateTime.Parse(txtDepartureDate.Text);
TimeSpan span = depart - arrive;
//days between dates
txtDays.Text = span.ToString();
}

*NEVERMIND I FIGURED IT OUT, HAVING TROUBLE DELETING THE QUESTION, NO LONGER NEED A RESPONCE
Sorry, finally found the button to insert the code, I'm used to the brackets icon stackoverflow has, didn't know the code button was in the drop down menu. I fixed my post as well as the solution! :)
 
The TotalDays property would have given you the time span value in days. Then it's up to you how you want to format the number as a string.
 
Why not use DateTimePicker controls rather than TextBoxes? There's no parsing required and there's not possibility of invalid input, which your code currently ignores.
 
*Updated code with solution I found that worked for me
Never edit your original post, unless its within the time frame of five to ten minutes, and providing you are only editing a typo. Especially do not edit your original post to update it with an answer. Instead, please reply to your thread by using the big reply area at the bottom of this page. If you edit your original post, people who have already replied begin to look like nutjobs who just came from the local asylum, talking about code and a problem that no longer exists.

John, do you have the ability to revert the original post?
 
I changed my code to this and it fixed the problem.
C#:
/*Used %d to the span of days gives back a single number in the textbox*/
txtDays.Text = span.ToString("%d");
Why not use DateTimePicker controls rather than TextBoxes? There's no parsing required and there's not possibility of invalid input, which your code currently ignores.
I could do that but I had to use text boxes because the assignment required me to.
 
Back
Top Bottom