Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Sarah 7 posts 77 karma points
    Jun 02, 2017 @ 14:52
    Sarah
    0

    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.

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Jun 02, 2017 @ 15:10
    Nik
    100

    Hi Sarah,

    I think that your best bet may be something similar to that.

    DateTime.Parse(TbToDate.Text).Date.AddDays(-1).Subtract(DateTime.Parse(TbFromDate.Text).Date).TotalDays));
    

    It's not the most elegant solution, but it would achieve what you are after.

    Thanks,

    Nik

  • Sarah 7 posts 77 karma points
    Jun 02, 2017 @ 15:27
    Sarah
    0

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

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Jun 02, 2017 @ 15:37
    Nik
    0

    Oops, that was my bad, I meant to subtract a day from the start date sorry about that.

    Well done on getting it working :-)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

    Continue discussion

Please Sign in or register to post replies