Mvc Find Specific Day from Start date and End Date

View : Calender.cshtml

  @{
    ViewBag.Title = "Calender";
}
<script src="~/Scripts/jquery-1.7.1.min.js"></script>
<script>
    function data() {

        var sdate = document.getElementById('sdate').value;
        var edate = document.getElementById('edate').value;


        var ctr = document.getElementById('days');
        var days = ctr.options[ctr.selectedIndex].text;

        var stt = document.getElementById('week');
        var week = stt.options[stt.selectedIndex].text;


        $.ajax({
            url: '@Url.Action("Calender", "Employee")',
            data: { sdate: sdate, edate: edate, days: days, week: week },
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                alert(data);
            }
        });
    }
</script>
<h2>Calender</h2>
<table>
<tr>
<td> Start Date</td><td> <input type="date" id="sdate" /></td>
</tr>
<tr>
<td> End Date</td><td> <input type="date" id="edate" /></td>
</tr>
<tr>
<td>Days</td>
<td> @Html.DropDownList("days",ViewBag.days as SelectList, "-Select -", new { style = "width:70px;height:25px", @id = "days" } )  </td>
</tr>
<tr>
<td>Option  </td>
<td>@Html.DropDownList("week",ViewBag.week as SelectList, "-Select -", new { style = "width:70px;height:25px", @id = "week" } )   </td>
</tr>
<tr>
<td></td><td> <input type="button" value="Submit" onclick="data();"/></td>
</tr>
<tr>
<td></td><td></td>
</tr>
</table>



Controller :

 [HttpGet]
        public ActionResult Calender()
        {
            List<SelectListItem> Dept1 = new List<SelectListItem>();
       
                Dept1.Add(new SelectListItem { Value ="1", Text ="Monday" });
                Dept1.Add(new SelectListItem { Value = "2", Text = "Tuesday" });
                Dept1.Add(new SelectListItem { Value = "3", Text = "Wednesday" });
                Dept1.Add(new SelectListItem { Value = "4", Text = "Thursday" });
                Dept1.Add(new SelectListItem { Value = "5", Text = "Friday" });
                Dept1.Add(new SelectListItem { Value = "6", Text = "Saturday" });
                Dept1.Add(new SelectListItem { Value = "7", Text = "Sunday" });
                ViewBag.days = Dept1;

                List<SelectListItem> Dept2 = new List<SelectListItem>();

                Dept2.Add(new SelectListItem { Value = "1", Text = "1" });
                Dept2.Add(new SelectListItem { Value = "2", Text = "2" });
                Dept2.Add(new SelectListItem { Value = "3", Text = "3" });
                Dept2.Add(new SelectListItem { Value = "4", Text = "4" });
                ViewBag.week = Dept2;

                return View();
           }

        [HttpPost]
        public ActionResult Calender(string sdate, string edate, string days, string week)
        {
            DateTime startDate = Convert.ToDateTime(sdate);
            DateTime endDate = Convert.ToDateTime(edate);
            TimeSpan diff = endDate - startDate;
            int count = 0;        
            string Fst = "";
            int day = diff.Days;
            for (var i = 0; i <= day; i++)
            {
                string st = "";
                var testDate = startDate.AddDays(i);
                if (testDate.DayOfWeek.ToString() == days)
                {
                    st = testDate.ToShortDateString();
                    count = count + 1;
                }
                if (count == int.Parse(week))
                {
                    Fst += st;
                }
             
            }
            if (String.IsNullOrEmpty(Fst))
            { Fst += "This Week Not available between Selected Date"; }
         
            return new JsonResult { Data = Fst, JsonRequestBehavior = JsonRequestBehavior.AllowGet };              
        }
Previous
Next Post »