I'm working on a school project right now, and we are building a booking system which displays available times for only today. (We're not using a calendar). My question is how do I only display available hours for today and not display the passed hours? Right now the code displays all the times from 8.00 to 16.00 even if the real time is 12.00. If the clock is 12 I would only like to display the hours after 12. I hope that you can help me, because I haven't found a solution that suits me.
This is how the code looks like in the View:
@{
int open = 8;
decimal inHours = Convert.ToDecimal(Model.service.Duration) / Convert.ToDecimal(60);
int iterations = (int)Math.Floor(Convert.ToDecimal(open) / Convert.ToDecimal(inHours));
DateTime startTime = DateTime.Today;
startTime = startTime.AddHours(8);
List<DateTime> dt = new List<DateTime>();
for (int i = 0; i < iterations; i++)
{
DateTime endTime = startTime;
endTime = endTime.AddMinutes(Model.service.Duration);
if (!Model.service.Bookings.Any(x => x.StartTime == startTime))
{
@Html.ActionLink(startTime.ToString("HH:mm") + "-" + endTime.ToString("HH:mm"),
"BookService", "Booking", new
{
inBookingSystemId = Model.bookingSystem.BookingSystemId,
inServiceId = Model.service.ServiceId,
inStartTime = startTime.ToString()
}, new { @class = "btn btn-primary" })
}
startTime = endTime;
}
}
To display only the further hours of the day you could do something like this:
List<DateTime> dt = new List<DateTime>();
int hoursToAdd = 1;
int hourNow = DateTime.Now.Hour;
for (int i = hourNow ; i <= 23; i++)
{
dt.Add(DateTime.Now.AddHours(hoursToAdd));
hoursToAdd++;
}
It is not the best way, there might be a more elegant way to do it, but it will work.