Note that there are some explanatory texts on larger screens.

plurals
  1. POExcluding dates from period using Nodatime
    primarykey
    data
    text
    <p>I'm trying to workout the amount of time between two <code>LocalDateTime</code> values and exclude specific dates (in this example, it's bank holidays).</p> <pre><code>var bankHolidays = new[] { new LocalDate(2013, 12, 25), new LocalDate(2013, 12, 26) }; var localDateTime1 = new LocalDateTime(2013, 11, 18, 10, 30); var localDateTime2 = new LocalDateTime(2013, 12, 29, 10, 15); var differenceBetween = Period.Between(localDateTime1, localDateTime2, PeriodUnits.Days | PeriodUnits.HourMinuteSecond); </code></pre> <p>The <code>differenceBetween</code> value shows the number of days/hours/minutes/seconds between the two dates, as you would expect. </p> <p>I could check every single day from the start date and see if the <code>bankHolidays</code> collection contains that date e.g.</p> <pre><code>var bankHolidays = new[] { new LocalDate(2013, 12, 25), new LocalDate(2013, 12, 26) }; var localDateTime1 = new LocalDateTime(2013, 11, 18, 10, 30); var localDateTime2 = new LocalDateTime(2013, 12, 29, 10, 15); var differenceBetween = Period.Between(localDateTime1, localDateTime2, PeriodUnits.Days | PeriodUnits.HourMinuteSecond); var london = DateTimeZoneProviders.Tzdb["Europe/London"]; for (var i = 1; i &lt; differenceBetween.Days; ++i) { var x = localDateTime1.InZoneStrictly(london) + Duration.FromStandardDays(i); if (bankHolidays.Any(date =&gt; date == x.Date)) { //subtract one day for the period. } } </code></pre> <p>I feel like I'm missing some obvious and there should be an easier method, is there a simpler way to find a period between two dates whilst excluding certain dates?</p> <p>I also need to include weekends in this exclusion too, the obvious way seems to be to check the day of the week for weekends whilst checking bank holidays, this just doesn't seem like the best/correct way of handling it though.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload