Check if date range is within another date range for fullcalendar.io display

守給你的承諾、 提交于 2019-12-24 05:11:22

问题


I'm implementing the server side code to fetch events for fullcalendar.io.

There are two scenarios:

  1. Event has a start and end date
  2. Event has a start date, and it's set to all day

Model:

public Class CalendarEvent {
  public DateTimeOffset Start { get; set; }
  public DateTimeOffset? End { get; set; }
  public bool AllDay { get; set; }
}

When https://fullcalendar.io loads a view, it emits a date range, which is basically the days that can be seen on the calendar (i.e. 10/8/18 to 12/8/18) and I send that to my controller to fetch relevant events for that range.

I basically need to check the following:

  1. Does any part of the event's date range fall within the visible range emitted from the calendar - if so, show it.
  2. If it's an AllDay event, does the start date fall within the view.

Pseudo Example:

var rep = context.GetRepository<Event>();
events = rep.Get().Where(e => /* need help here */).AsQueryable();

回答1:


I like this one (overlapping dates)! In general the formula is:

var dateFrom = ;// start of week
var dateTo = ; // end of week

var events = rep.Get()
    .Where(e => e.Start <= dateTo && e.End >= dateFrom)
    .AsQueryable();

Because you need things contained by the range, starting in the range and extending out, starting before the range and ending in, and starting before and ending after the range.

But if you need to handle a nullable .End then maybe more like:

var dateFrom = ;// start of week
var dateTo = ; // end of week

var events = rep.Get()
    .Where(e => e.Start <= dateTo && (e.End ?? e.Start) >= dateFrom)
    .AsQueryable();

(very) rudimentary mockup to help visualize:



来源:https://stackoverflow.com/questions/53453381/check-if-date-range-is-within-another-date-range-for-fullcalendar-io-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!