C#: Calculate the difference between 2 times

NerishaB
NerishaB used Ask the Experts™
on
Hi,

I have 2 DateTiime fields, ClockTime, and DefaultStartTime.

The ClockTime is the actual time a person clocked into the office, and the DefaultStartTime is the time that work actually starts for the day.  The fields are as follows:

ClockTime                                 DefaultStartTime
2011/04/01 07:35:23                2011/04/03 08:00:00

Now, even though the Date of ClockTime and DefaultStartTime are different, I actually dont want to take the dates into consideration.  I just want to calculate the difference between the times ie. 08:00:00 - 07:35:23.  Can anyone help?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi,

You can use TimeOfDay to get the time only such as this:

Good Luck!
DefaultStartTime.TimeOfDay - ClockTime.TimeOfDay

Open in new window

Author

Commented:
Thanks, and how will I convert the result to a double?

My code is as follows:

TimeSpan ResultTime = DefaultStartTime.TimeOfDay - ClockTime.TimeOfDay

I now want to convert ResultTime to a double.


Commented:
What should the double represent?

If it should represent seconds then just do:

double totalSeconds = ResultTime.TotalSeconds;

Open in new window

OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Commented:
Try something like this:

string ResultTimeAsDouble = ResultTime.ToString().Replace(":", "");
double ResultTimeAsDouble = Convert.ToDouble(ResultTimeAsDouble);

Open in new window


Didn't try it but it should give you doubles that look like 073523 which should be easily calculatable.

Author

Commented:
thanks a mil.

Commented:
Oops.. corrected:

string ResultTimeAsString = ResultTime.ToString().Replace(":", "");
double ResultTimeAsDouble = Convert.ToDouble(ResultTimeAsString);

Open in new window


I shouldn't really do this without access to my visual studio :(

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial