How to compare two dates in C# and get IN & OUT time of employee?


Below is my table. I want to compare between two dates which have the same employee_id. It's an attendance system. Suppose according to the table, for employee_id =2, in time is 10:00:00 and out time is 17:41:00. Now how can I show in Index view that which one is intime of an employee and which one is outtime of an employee?


Asked by:- SalmanZahir
0
: 4084 At:- 2/4/2018 7:27:13 AM
asp.net mvc compare-two-dates compare-two-time C#







3 Answers
profileImage Answered by:- Sam

Looks like you didn't got my answer, you need to do some changes according to your need always, that was sample code to make it easier for you, here is my complete solution please modify it according to your use, database table etc

  //Create a EmployeeAtt class List to show data in VIew
                List<EmployeeAtt> empAttList = new List<EmployeeAtt>();

                //Create a EmployeeAtt class
                EmployeeAtt empatt = new EmployeeAtt();

                //Get All Employee
                var AllEmp = context.tbl_employee.ToList();

                //loop through each emp
                foreach (var emp in AllEmp)
                {
                   
                    //Get Array list for each emp using it's Id and sort list according to datetime
                    var attendnaceList = context.attendance_table.Where(a => a.employee_id == emp.employee_id).OrderBy(a => a.date_and_time).ToArray();

                //Get Count
                var Count = attendnaceList.Count();

                //Loop through all values and increment it by 2 as we will compare two values in/out
                for (var i = 0; i < Count; i = i + 2)
                {
                    //create new employee object fo each loop
                    empatt = new EmployeeAtt();
                    //Although no need to check but confirm if both the values are of same date,which we will compare
                    if (attendnaceList[i].date_and_time.Value.Date == attendnaceList[i + 1].date_and_time.Value.Date)
                    {
                        //Check which value is less using Time now,as it is sorted  first value will be less than second
                        if (attendnaceList[i].date_and_time.Value.TimeOfDay < attendnaceList[i + 1].date_and_time.Value.TimeOfDay)
                        {
                                empatt.Id = Convert.ToInt32(emp.employee_id);
                                empatt.Emp_name = emp.employee_name;
                                empatt.Date = attendnaceList[i].date_and_time.Value.Date;

                                //OUttime Greater one
                                empatt.OutTime = attendnaceList[i + 1].date_and_time.Value.TimeOfDay;

                                //In time less value
                                empatt.InTime = attendnaceList[i].date_and_time.Value.TimeOfDay;
                                empAttList.Add(empatt);

                        }
                    }
                }

               }

In the above code, I have taken complete employee list then loop through each employee and then getting only it's data and saving it in a List empAttList of type EmployeeAtt , EmployeeAtt class is as below

public class EmployeeAtt
    {
        public int Id { get; set; }
        public string Emp_name { get; set; }
        public DateTime Date { get; set; }
        public TimeSpan InTime { get; set; }
        public TimeSpan OutTime { get; set; }
    }

Now I am rendering IEnumerable<ProjectMVC.Models.EmployeeAtt> model in view, so here is the output of view

output-view-in-out-time-min(1).png

Here are  my demo tables, based on your requirement, First table has Employee Id, In/Out time and date

demo-table1-for-224-min.png

Second Table has Employee Id, Name

demo-table2-for-224-min.png

You can see in the above example we have "Test Test" employee has 2 different date and different data for IN/OUT time.

3
At:- 2/5/2018 6:22:16 PM Updated at:- 2/5/2018 6:25:19 PM
solved the problem. Thank you so much for your guidelines. Although I just had to change one line. I forgot that I was taking the attendance[i] not attendance[i+1] which I mistakenly took for the outtime. That's why in view, outtime was shown 00:00:00 0
By : SalmanZahir - at :- 2/6/2018 4:57:58 AM
Sorry I mistakenly downvoted. Can't change the value anymore :( This answer deserves upvote. 2
By : SalmanZahir - at :- 2/6/2018 4:59:32 AM


profileImage Answered by:- Sam

You can compare two date and then it's time, suppose you need two compare values of same id

  1. First get list of all the values for same employee _id, and sort them by date_and_time
  2. Compare Dates if they are equal
  3. Check which time is less than the other, time which is less means it is IN time while the greater one is OUT time

Here is the complete code in C#, I assume here table name is  attendance_table & employee_id==2, so your code will to get In and Out time will be

                //Get Array list for Employee_id=2 and sort list according to datetime
                var attendnaceList = context.attendance_table.Where(a=>a.employee_id==2).OrderBy(a=>a.date_and_time).ToArray();

                //Get Count
                var Count = attendnaceList.Count();

                //Loop through all values and increment it by 2 as we will compare two values in/out
                for(var i=0; i< Count; i=i+2)
                {
                    //Although no need to check but confirm if both the values are of same date,which we will compare
                    if(attendnaceList[i].date_and_time.Value.Date== attendnaceList[i+1].date_and_time.Value.Date)
                    {
                        //Check which value is less using Time now,as it is sorted  first value will be less than second
                        if(attendnaceList[i].date_and_time.Value.TimeOfDay < attendnaceList[i + 1].date_and_time.Value.TimeOfDay)
                        {
                            //OUttime Greater one
                            var OutTime = attendnaceList[i + 1].date_and_time.Value.TimeOfDay;

                            //In time less value
                            var inTIme = attendnaceList[i].date_and_time.Value.TimeOfDay;
                        }
                    }
                }

I have explained the code in the comments and it works for me.

5
At:- 2/4/2018 4:23:32 PM
Thank you so much. But can you please tell me how can I do it dynamically? I mean you have put employee_id as 2 here. For all the employees, what should I do? 0
By : SalmanZahir - at :- 2/5/2018 5:13:46 AM


profileImage Answered by:- SalmanZahir

img1

This is my Table in the view. I want to show InTime and OutTime written beside every Time_and_date column row. 

0
At:- 2/5/2018 5:48:00 AM Updated at:- 2/5/2018 5:49:09 AM
As I understand from the above table, your table is linked from the employee_table, which has unique id(employee_id) for each employee, loop through all the employee and get id of each employee and then use above @Sam's answer, it should work for each employee then. 0
By : vikas_jk - at :- 2/5/2018 7:01:32 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use