public void TimeSearch(SearchWindow window) { int selectedUserId = Convert.ToInt32(window.CbSearchTimesUserId.SelectedValue.ToString()); DateTime?selectedStartDate = window.DpSearchTimesStartDate.SelectedDate; DateTime?selectedEndDate = window.DpSearchTimesEndDate.SelectedDate; List <WorkPeriod> periods = new WorkPeriodCRUD().GetWorkPeriods(); if (selectedUserId > 0) { periods.RemoveAll(x => x.UserId != selectedUserId); } if (selectedStartDate != null) { periods.RemoveAll(x => x.Start >= Convert.ToDateTime(selectedStartDate)); } if (selectedEndDate != null) { periods.RemoveAll(x => x.End <= Convert.ToDateTime(selectedEndDate)); } if (periods.Count > 0) { BindDataGrid(periods); window.Close(); } else { MessageBox.Show("There are no results", "Information"); } }
public string GetWeeklyWorkPeriodReadableForUser(User user) { DateTime startOfWeek = DateTime.Now; DateTime endOfWeek = DateTime.Now; string readable = ""; // get start of week while (startOfWeek.DayOfWeek != System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek) { startOfWeek = startOfWeek.AddDays(-1); } // get end of week while (endOfWeek.DayOfWeek != System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek) { endOfWeek = endOfWeek.AddDays(1); } // pull work periods between start and end of week List <WorkPeriod> workPeriods = new WorkPeriodCRUD().GetWorkPeriodsByUserId(user.UserId, startOfWeek, endOfWeek); if (workPeriods.Count > 0) { // remove any unfinished work periods workPeriods.RemoveAll(x => x.End == null); // if any work periods are left... if (workPeriods.Count > 0) { TimeSpan total = new TimeSpan(); // look through the work periods... foreach (WorkPeriod period in workPeriods) { // subtract the end date from the start date to get total hours in the work period TimeSpan span = Convert.ToDateTime(period.End).Subtract(period.Start); // add that result to the total timespan total += span; } readable = total.Hours + " hours and " + total.Minutes + " minutes"; } else { readable = "No Hours, Clocked In"; } } else { readable = "No Hours"; } return(readable); }