Calculate hours difference between two Times in C# in ASP.Net

hamzashah5
 
on Dec 01, 2020 04:31 AM
2592 Views

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)

Suppose employee's Checkin time is 09:30 AM and CheckOut time is 06:30pm. So how can i perform the operation to calculate the total working hours while the format is in AM PM

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 01, 2020 05:04 AM

Convert the time to DateTime to calculate the difference.

Example

string checkIn = "09:30 AM";
string checkOut = "06:30pm";
double diff = Convert.ToDateTime(checkOut).Subtract(Convert.ToDateTime(checkIn)).TotalHours;