Hamza Shah

Hamza Shah

  • NA
  • 87
  • 23k

In date range Start date should be one month before the current date

Nov 16 2020 5:33 AM
In my attendance portal, I've set the date range "Start" to "End". In these two fields I've used date picker so the admin can search the attendances from Whatever date he selected to whatever date he wanted. But I want my start date value to be one month before the current date. I've searched it a lot but can't find better one.
 
Here's my View code
  1. <div class="row">  
  2. <div class="form-group">  
  3. <div class="col-md-3">  
  4. @Html.DropDownList("Employee",  
  5. (IEnumerable<SelectListItem>)TempData["Employees"],  
  6. "All Employee",  
  7. new { @class = "form-control", onchange = "this.form.submit();" })  
  8. </div>  
  9. <div class="col-md-3">  
  10. <input id="start" autocomplete="off" name="start" class="form-control datepicker" placeholder="Start" value="@startDate" />  
  11. </div>  
  12. <div class="col-md-3">  
  13. <input id="end" class="form-control datepicker" autocomplete="off" name="end" placeholder="End" value="@endDate" />  
  14. </div>  
  15. <div class="col-md-3">  
  16. <input type="submit" value="Search" class="btn btn-success" />  
  17. </div>  
  18. </div>  
  19. </div>  
And here's my controller code
  1. [HttpPost]  
  2. [ValidateAntiForgeryToken]  
  3. public ActionResult All(int? Employee, int? page, string start, string end)  
  4. {  
  5. List<Attendance> attendanceList;  
  6. if (Employee != null)  
  7. {  
  8. if (start != "" && end != "")  
  9. {  
  10. DateTime dtstart = Convert.ToDateTime(start);  
  11. DateTime dtend = Convert.ToDateTime(end);  
  12. attendanceList = db.Attendance.ToList().Where(x => x.Emp_Id == Employee && x.Date >= dtstart && dtend >= x.Date).ToList();  
  13. }  
  14. else  
  15. {  
  16. attendanceList = db.Attendance.ToList().Where(x => x.Emp_Id == Employee).ToList();  
  17. }  
  18. return View(attendanceList);  
  19. }  

Answers (3)