Hamza Shah

Hamza Shah

  • NA
  • 87
  • 21.4k

Calculate the working hours of an employee while format is in 12 Hours

Dec 1 2020 5:44 AM
In my attendance portal, employee marks the attendance by giving CheckIn time and CheckOut time. I am using Bootstrap datetimepicker Where the time format is in 12 Hour (AM , PM). i want to calculate the total working hours per day of the employee but i'm facing the datetime format issues. So how can i calculate the total working hours per day of the employee when the format is in 12 hours (AM , PM)
 
Here's my View Code
  1. <div class="col-md-3">  
  2. <label>Date <span class="text-danger">*</span></label>  
  3. <div id="datepicker" class="input-group date" data-link-field="dtp_input2" data-date-format="dd/mm/yyyy" data-link-format="dd/mm/yyyy">  
  4. @*<input class="form-control" type="text" readonly />*@  
  5. @Html.TextBoxFor(model => model.Date, new { @class = "form-control", autocomplete = "off" })  
  6. <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>  
  7. <span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>  
  8. </div>  
  9. <input type="hidden" id="dtp_input2" value="" /><br />  
  10. </div>  
  11. <div class="col-md-3">  
  12. <label>Check In <span class="text-danger">*</span></label>  
  13. @*<input type="datetime-local" id="CheckIn" name="CheckIn">*@  
  14. <div class="input-group date form_datetime" data-date="" data-date-format="mm/dd/yyyy HH:ii p" data-link-field="dtp_input1" data-link-format="mm/dd/yyyy HH:ii ">  
  15. @Html.TextBoxFor(model => model.CheckIn, new { @class = "form-control", autocomplete = "off", required = "required" })  
  16. <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>  
  17. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>  
  18. </div>  
  19. <input type="hidden" id="dtp_input1" value="" /><br />  
  20. </div>  
  21. <div class="col-md-3">  
  22. <label>Check Out</label>  
  23. @*<input type="datetime-local" id="CheckOut" name="CheckOut">*@  
  24. <div class="input-group date form_datetime2" data-date="" data-date-format="mm/dd/yyyy HH:ii p" data-link-field="dtp_input3" data-link-format="mm/dd/yyyy HH:ii ">  
  25. @Html.TextBoxFor(model => model.CheckOut, new { @class = "form-control", autocomplete = "off" })  
  26. <span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>  
  27. <span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>  
  28. </div>  
  29. <input type="hidden" id="dtp_input3" value="" /><br />  
  30. </div>  
  31. <div class="row">  
  32. <div class="col-md-12">  
  33. <input type="button" id="save" class="btn btn-primary" value="Mark Attendance" />  
  34. @Html.ActionLink("My Attendance""Index""Attendance")  
  35. </div>  
  36. </div>  
  37. <script>  
  38. $("#save").click(function () {  
  39. var obj = {  
  40. Emp_Id : $(Emp_Id).val(),  
  41. Date: $("#Date").val(),  
  42. CheckIn: $("#CheckIn").val(),  
  43. CheckOut: $("#CheckOut").val(),  
  44. };  
  45. //window.location.href = "/path/to/thankyoupage"  
  46. var Url = '@Url.Action("Create", "Attendance")';  
  47. $.ajax({  
  48. url: Url,  
  49. type: "POST",  
  50. data: obj,  
  51. dataType: "html",  
  52. success: function (data) {  
  53. if (data.isError==false) {  
  54. alert(data.message);  
  55. }  
  56. }  
  57. });  
  58. })  
  59. </script>  
And Here's my Controller Code
  1. [HttpPost]  
  2. public JsonResult Create(Attendance attendance)  
  3. {  
  4. // if (attendance.Date == todayDate) return BadRequest("Too late or too early");  
  5. // var hasAttendance = db.Attendance.Any(i => i.Emp_Id = attendance.Emp_Id && i.Date == todayDate);  
  6. //DateTime tempDate = Convert.ToDateTime(attendance.Date);  
  7. if (ModelState.IsValid)  
  8. {  
  9. try  
  10. {  
  11. attendance.CheckIn = Convert.ToDateTime(attendance.CheckIn.ToString("yyyy-MM-dd HH:mm:ss"));  
  12. attendance.CheckOut = Convert.ToDateTime(attendance.CheckOut.Value.ToString("yyyy-MM-dd HH:mm:ss"));  
  13. attendance.Date = DateTime.Now;  
  14. var attdate = attendance.Date;  
  15. var nextdate = attdate.AddDays(1);  
  16. var id = Convert.ToInt32(Session["UserID"]);  
  17. var isExist = db.Attendance.FirstOrDefault(i => i.Emp_Id == id && i.Date == attdate && i.Date < nextdate);  
  18. if (isExist != null)  
  19. {  
  20. //return Content("<script language='javascript' type='text/javascript'>alert('Your Attendance is Already Marked');</script>");  
  21. //ViewBag.isExist = false;  
  22. //TempData["Msg"] = ("Your Attendance is Already Marked");  
  23. var kMessage = "Your Attendance is Already Marked";  
  24. //return RedirectToAction("Index", "Attendance", new { message = kMessage });  
  25. return Json(new { isError = false, message = kMessage });  
  26. }  
  27. else  
  28. {  
  29. //attendance.Emp_Id = id;  
  30. //var res = tempDate.Date;  
  31. db.Attendance.Add(attendance);  
  32. db.SaveChanges();  
  33. ViewBag.kMessage = "";  
  34. }  
  35. }  
  36. catch (Exception ex)  
  37. {  
  38. Console.WriteLine(ex.InnerException.Message);  
  39. }  
  40. }  
  41. return Json(new {isError=false,message="YAAAAAAAAAAAAY" });  
  42. //return RedirectToAction("Index", "Attendance");  
  43. }

Answers (1)