Avatar of NerishaB
NerishaBFlag for South Africa

asked on 

C#: Calculate the difference between 2 times

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?
C#

Avatar of undefined
Last Comment
h4mi
Avatar of oxyoo
oxyoo
Flag of Sweden image

Hi,

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

Good Luck!
DefaultStartTime.TimeOfDay - ClockTime.TimeOfDay

Open in new window

Avatar of NerishaB
NerishaB
Flag of South Africa image

ASKER

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.


ASKER CERTIFIED SOLUTION
Avatar of oxyoo
oxyoo
Flag of Sweden image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of h4mi
h4mi
Flag of Sweden image

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.
Avatar of NerishaB
NerishaB
Flag of South Africa image

ASKER

thanks a mil.
Avatar of h4mi
h4mi
Flag of Sweden image

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 :(
C#
C#

C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

98K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo