How to calculate hours between days?

Hi, let me show you first what i’m doing to understand what i want to do…

I have 2 variables datetime types:
initDate=now
endDate=now.AddHours(2)

I’m writing in console with write line this result:
(endDate.Subtract(initDate)).ToString(“hh:mm:ss”)

This will gave me this result : 02:00:00

Now I’ve made some changes:
endDate=now.AddHours(2).addDays(2)

I was hoping to have this result → 50:00:00 (24 hours per day + 2 hours)
But instead I’ve received this → 02:00:00

It looks like is not reading difference between days.
Do you know what’s the right way to do have the complete hours between 2 days and 2 hours more?

Thank you a lot

@KevinDS
(endDate-initDate) will give you the TimeSpan between your two date times. The timespan is 2 days and 2 hours, so when you use the Hours method you get 2. If you instead use TotalHours it will count all hours in the TimeSpan.

image

image

1 Like

Hi @DanielMitchell thank you for the Reply :slight_smile:

we’re almost there.

The problem with TotalHours is that creates a decimal.
if I put now this:
endDate=now.AddHours(2).addDays(2).addMinutes(-18)

it gaves me: 49,7xxxxxxxxxx (x means a lot of numbers)

When I need to have something like (“HH:mm:ss”) : 49:42:00

It’s like when you’re doing a countdown from one date to other date.

I’ll try to search like a countdown to know If I can found something…

Thank you :slight_smile:

@KevinDS - TotalHours is of course just going to provide the hours. It sounds like you want to get down to the minute. To do that get the timespan (i’ll call ts), but you’ll need 2 separate integer variables I’ll call hours and minutes. assign hours = Math.Floor(ts.TotalHours) and assign minutes = ts.Minutes. Now just print it out as write line = hours.tostring + “:” + minutes.tostring " + “:00”

2 Likes

@DanielMitchell I’ve Got it!

I’ve been guide by this topyc And in “variable” I’ve put what you told me and work :slight_smile:

1 Like

Thank you @Dave :slight_smile: Just at the same time but glad to have your help also

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.