Display filtered dates from a given date range, excluding weekends and specific dates

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) { // Copy date objects so don't modify originals var s = new Date(start); var e = new Date(end); var addOneMoreDay = 0; if (s.getDay() == 0 || s.getDay() == 6) { addOneMoreDay = 1; } // Set time to midday to avoid dalight saving and browser quirks s.setHours(12, 0, 0, 0); e.setHours(12, 0, 0, 0); // Get the difference in whole days var totalDays = Math.round((e - s) / 8.64e7); // Get the difference in whole weeks var wholeWeeks = totalDays / 7 | 0; // Estimate business days as number of whole weeks * 5 var days = wholeWeeks * 5; // If not even number of weeks, calc remaining weekend days if (totalDays % 7) { s.setDate(s.getDate() + wholeWeeks * 7); while (s < e) { s.setDate(s.getDate() + 1); // If day isn't a Sunday or Saturday, add to business days if (s.getDay() != 0 && s.getDay() != 6) { ++days; } //s.setDate(s.getDate() + 1); } } var weekEndDays = totalDays - days + addOneMoreDay; return days + 1; } var date1 = '@ViewBag.StartDate'; var date2 = '@ViewBag.EndDate'; 

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)

0
source share
1 answer

To disable the weekend from the date picker, you can simply:

 $("#from").datepicker({ beforeShowDay: $.datepicker.noWeekends }); 

To disable specific dates, you can also use beforeShowDay :

 var dateArray= ["2017-11-22","2017-11-22","2017-11-22"] $('input').datepicker({ beforeShowDay: function(date){ var test = jQuery.datepicker.formatDate('yy-mm-dd', date); return [ sArray.indexOf(test) == -1 ] } }); 

You can use the latter method to exclude holidays by including it in your dates array.

Here is a link to the jQuery API for beforeShowDay: http://api.jqueryui.com/datepicker/#option-beforeShowDay

Hope this helps.

0
source

Source: https://habr.com/ru/post/981891/


All Articles