I am trying to make an application that can add dates for hotel rooms and get the average time between the dates. I have made 2 calendars, 1 for the starting date and the other one for the ending date. When I show the output in the listbox, I don't know how to get the days between this dates. I need them to calculate the average.
I've tried to get the dates but I'm confused with the datetime things.
lbDates.Items.Add(string.Format("{0}, {1}, {2}, {3}", cmbBoxRoom.SelectedItem, tbBooking.Text, calArrival.SelectedDate, calDeparture.SelectedDate));
How can I get the between of the dates so I can calculate with that?
Please refer the below code
DateTime a = DateTime.Now;
Console.WriteLine("Start Date: " +Convert.ToString(a));
DateTime b = DateTime.Now.AddDays(5).AddHours(5);
//b = b.ToUniversalTime();
Console.WriteLine("End Date: " +Convert.ToString(b));
TimeSpan result = b - a;
//Your code goes here
Console.WriteLine(Convert.ToString(result));
Two variable a & b takes values for start and end date. Result wil show the output difference between both in Days.Hours:Minutes:Seconds
Check below link
It looks like you simply want to get the time between two dates. In C# this is trivially easy using the DateTime.Subract()
method like so:
TimeSpan deltaTime = calDeparture.SelectedDate.Subtract(calArrival.SelectedDate);
or you can use the plain old -
operator
Timespan deltaTime = calDeparture.SelectedDate - calArrival.SelectedDate;
This is the same as using the Subtract
Method
The deltaTime
time variable is a TimeSpan holding the amount of time in between the two dates, you can now (for example) do deltaTime.Days
to get the amount of days in between the two dates
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.