Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I am trying to build an hotel management project where bill is to be calculated on the basis of number of days stayed.
I have used date time picker control to insert date and time to my textbox into string format

C#
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
       {
      txtdatein.Text = dateTimePicker1.Value.ToString();
      dateTimePicker1.Visible = false;
       }

so is it possible to calculate number of days.
Posted
Updated 17-Apr-12 20:04pm
v2

Hi.

only takes one line.
(toDate - fromDate).Days

or

This can be easily accomplished using an object of Type "TimeSpan". For example: let's assume that we want to know the number of days between the max. and min. values for the DateTime Type and show it in a Console window, then I may write something like:

DateTime d1=DateTime.MinValue;
DateTime d2=DateTime.MaxValue;
TimeSpan span=d2-d1;
Console.WriteLine
( "There're {0} days between {1} and {2}" , span.TotalDays, d1.ToString(), d2.ToString() );
Note that I used the TotalDays property to get the number of days in between. This gets me the number of days putting in consideration the years with fraction days (years with 366 days). I could also use the property "Days" that would get me the value considering that all the years consist of 365 days only.

refer this link
http://r4r.co.in/c1/01/tutorial/csharp/Calculating%20Duration%20Between%20Two%20Dates%20in%20Years,%20Months%20and%20Days.shtml[^]
 
Share this answer
 
Comments
[no name] 18-Apr-12 17:58pm    
notice that you should use TotalDays, otherwise it can return 1.5 days :), dont know if a fractional value would be good for them. :)
shekhar2012 19-Apr-12 7:20am    
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
txtdatein.Text = dateTimePicker1.Value.ToLocalTime().ToString();
dateTimePicker1.Visible = false;

}

private void btndateout_Click(object sender, EventArgs e)
{
dateTimePicker2.Visible = true;
}

private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
{
txtdateout.Text = dateTimePicker2.Value.ToLocalTime().ToString();
dateTimePicker2.Visible = false;

}

i am still unable... the above mentioned code is what i have used to get date and time to the text boxes. now i want to calculate the difference between the days and show it in a text box. tried to use timespan error shows cant convert datetime to timespan.... plz help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900