问题
I'm implementing the server side code to fetch events for fullcalendar.io.
There are two scenarios:
- Event has a start and end date
- 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:
- Does any part of the event's date range fall within the visible range emitted from the calendar - if so, show it.
- 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