Home > Enterprise >  Getting today, past, and tomorrows list of appointments
Getting today, past, and tomorrows list of appointments

Time:09-17

I have a problem when it comes to getting dates and times to be displayed as a list on the view page of my application.

Example:

 public IList<Appointment> GetTodaysAppointments()
    {
        DateTime today = DateTime.Now.Date;

        return db.Appointments
            .Where(e => e.StartDateTimeBooking == today)
            .Include(e => e.Patient) 
            .ThenInclude(p => p.User)
            .Include(e => e.Doctor) 
            .ThenInclude(d => d.User)
            .OrderBy(d => d.StartDateTimeBooking)
            .ToList();
    }

This is my code and to save repeating code to get tomorrow and past appointments this would be the same outlook for the code.

I have tried implementing the operator "<". This works great for past appointments and will only display past appointments and nothing else.

The Issue: When I try to use "==" It will not display today's appointments or any appointment. However, If I use ">=", It will show today and upcoming appointments. This is great but I do not need it for tomorrow's appointments. I want tomorrow to be separate.

I did notice I am using DateTime which I do have Dates with time stored in my variable.

I know If I need to get today's appointments I want to do something like this:

StartDateTimeBooking >= YESTERDAY_MIDNIGHT && StartDateTimeBooking <= TODAY_MIDNIGHT

My Problem is my time is in 24 hours' time. What equation or code do I need today to ensure that: Today, past, and upcoming appointments will work?

I know I am very close to the solution but any help would be greatly appreciated.

UPDATE: Is this logic correct?

This gives me todays list of appointments:

 .Where(e => e.StartDateTimeBooking >= today && e.StartDateTimeBooking < today.AddDays(1))

This gives me tomorrows list of appointments only:

Where(e => e.StartDateTimeBooking > today.AddDays(1) && e.StartDateTimeBooking < today.AddDays(2))

This gives me an upcoming list of appointments

.Where(e => e.StartDateTimeBooking >= today)

This gives me past appointments:

.Where(e => e.StartDateTimeBooking < today)

CodePudding user response:

Because dates include times in the database, you need an in between filter on your date:

Assuming today is your date variable and represents a date at midnight (e.g. DateTime.Today):

.Where(e => e.StartDateTimeBooking >= today && e.StartDateTimeBooking < today.AddDays(1))

This creates the between filter you should need. It's midnight or after, but before tomorrow at midnight (AddDays(1)).

  • Related