Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time difference between two DateTime objects?

Tags:

c#

datetime

 emp = moduleEmployee.ReturnEmployeeDAO().FetchEmployeeByID(emp);

 DateTime shiftStartTime = emp.Shift.StartTime;
 DateTime shiftEndTime = emp.Shift.EndTime;

 DateTime attTime = att.Time;

According to the above code my shiftStartTime is 11.00 PM and shiftEndTime is 7.00 AM. and attTime or signIntime is 1.00 AM. then how to calculate the difference between shiftStartTime and attTime. where the difference should be 2 hour.

please help.....

Thanks Rusho

like image 305
PAryan Avatar asked Jul 07 '26 00:07

PAryan


1 Answers

If you subtract two DateTime objects, you get a TimeSpan.

A TimeSpan then has properties for TotalMilliseconds, TotalSeconds, etc.

You want the TimeSpan.TotalHours

 int shiftHours = (attTime - shiftStartTime).TotalHours;
like image 70
Neil Fenwick Avatar answered Jul 08 '26 13:07

Neil Fenwick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!