Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

calculate the difference between two UTC times in hours C#

How do I calculate the difference between two UTC times in hours?

This is what I have tried:

lastSuccessfulRunTime // UTC Time
int timeDifference = (DateTime.UtcNow - lastSuccessfulRunTime).Hours

if (DateTime.UtcNow – lastSuccessfulRunTime) = 9.00:00:00.7944388, timeDifference will be 0 which is not what I need.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The Hours property returns the integer hours value of the timespace.

i.e. for an hour and a half, it would give you 1.

The TotalHours property, converts the entire timespan into the double representation.

i.e. for an hour and a half, it would give you 1.5

You want:

double timeDifference = (DateTime.UtcNow - lastSuccessfulRunTime).TotalHours

(note, your int variable type is also wrong)

This applies to all of the time-component properties on a DateTime

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading