We are trying to get a duration between 2 dates to display a total number of days in an email we receive from a contact form. What we have at the moment is:
However, when we enter a date, e.g. 1/6/2017 and end date 3/6/2017 the duration shows as 2 days which isn't correct for our purpose as we need to include the first day as well, so the calculation shows as 3 days...any help would be appreciated. I guess we should be adding a +1 somewhere, but I'm very very new to ASP.Net.
Duration between 2 dates
We are trying to get a duration between 2 dates to display a total number of days in an email we receive from a contact form. What we have at the moment is:
ef.AddRow("Duration", String.Format("{0} Days", DateTime.Parse(TbToDate.Text).Date.Subtract(DateTime.Parse(TbFromDate.Text).Date).TotalDays));
However, when we enter a date, e.g. 1/6/2017 and end date 3/6/2017 the duration shows as 2 days which isn't correct for our purpose as we need to include the first day as well, so the calculation shows as 3 days...any help would be appreciated. I guess we should be adding a +1 somewhere, but I'm very very new to ASP.Net.
Hi Sarah,
I think that your best bet may be something similar to that.
It's not the most elegant solution, but it would achieve what you are after.
Thanks,
Nik
Thanks for your suggestions. Nik, thank you - this actually subtracted the days meaning a 3 day hire period was reduced to 1, but using:
DateTime.Parse(TbToDate.Text).Date.AddDays(+1).Subtract(DateTime.Parse(TbFromDate.Text).Date).TotalDays));
has worked beautifully. Thank you!!
Oops, that was my bad, I meant to subtract a day from the start date sorry about that.
Well done on getting it working :-)
is working on a reply...