How to get the time difference between two DateTime objects?
em开发者_如何学编程p = 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
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;
Just subtract one time from the other and you'll get a TimeSpan object.
see http://msdn.microsoft.com/en-us/library/1905yhe2.aspx
TimeSpan diff = att.Time.Subtract(emp.Shift.StartTime);
are you asking for something like this: ?
var timeSpan = (attTime - shiftStartTime);
the result is a TimeSpan where you can check how many hours, minutes, days, seconds and so on...
精彩评论