First, I will try to give a pictorial representation of the problem that I have:
- Maincontroller
- View Indexes
- View Logs
In the index view, a simple start date and end date are selected along with the identifier. Then, the MainController displays all dates between the start date and the end date using the snippet below;
Index view
<div class="row"> <div class="form-group"> <b>Start Date</b> <input type="date" id="startDate" name="startDate" required /> <b>End Date</b> <input type="date" id="endDate" name="endDate" required /> </div>
Maincontroller
var mod = (from m in lstMachineInfo where m.RegisterationId == EnrollNumber && m.Date >= startDate && m.Date <= endDate select m).Distinct(); var earlyMod = mod .GroupBy(dt => dt.Date) .Select(z => z.OrderBy(y => y.Date).First()) .ToList(); var presRowCount = 0; presRowCount = earlyMod.Count(); ViewBag.presRowCount = presRowCount; return View(earlyMod);
Dates are displayed in a table created as logs, for example:
View Logs
<th> @Html.DisplayNameFor(model => model.DateTime) </th> @foreach (var item in Model) { <td> @Html.DisplayFor(modelItem => item.DateTime) </td> }
Now I aim at the fact that instead of the dates shown only, I want the dates to show that:
- Exclude all weekends from the range (excluding all weekends, for example, Sat / Sun).
- Exclude dates that are already shown in the table (comparing days of the week with current dates and excluding current dates from it)
- Do you have any specific or several dates for public holidays. (will also be excluded from the list received before 2.) [this part may be ignored for now]
What I've done:
I loaded both startDate and endDate from Index View into MainController using;
startedDate = Convert.ToDateTime(form["startDate"]); endedDate = Convert.ToDateTime(form["endDate"]);
then I passed the dates to the log view using ViewBag
ViewBag.StartDate = startedDate; ViewBag.EndDate = endDate;
Using the function below, I can calculate (but I need to display dates) on weekdays between the two dates;
function dateDifference(start, end) {
Which is obviously not the function I need. I, however, also have a function that displays all dates between ranges, but this does not exclude weekends and dates that I already show from MainController;
$("#from").datepicker(); $("#to").datepicker(); // using Datepicker value example code $('#getBetween').on('click', function () { var start = $("#from").datepicker("getDate"), end = $("#to").datepicker("getDate"); var between = getDates(start, end); $('#results').html(between.join('<br> ')); }); // This function doing this work. function getDates(start, end) { var datesArray = []; var startDate = new Date(start); while (startDate <= end) { datesArray.push(new Date(startDate)); startDate.setDate(startDate.getDate() + 1); } return datesArray; }
Shortly speaking,
Show all dates in the range (start date and end date), except weekends. (DatesA = Calendar_picked dates - Weekends_in_those_dates)
Once this is done,
Display all dates that do not have a date already shown with MainCOntroller. (DatesFinal = DatesA - MainControllerDates)