273
Views
11
Comments
How can I get the difference between two hours, in 0 to 24 format?
Question

Example: If I have 23:50 and 00:10, I would like to get as a result of the difference "00:20". 


Or If I have 00:30 and 00:10, I would like to get "-00:20" (in negative).


There's a way to make it?


Thank you so much!

2026-02-26 06-29-24
Rahul
 
MVP

Hi Julia,

Have you tried BuitIn function- DiffMinutes()



Use this ,you will get result as you want.


Regards

Rahul Sahu

UserImage.jpg
Julia Freitas

Rahul Sahu wrote:

Hi Julia,

Have you tried BuitIn function- DiffMinutes()



Use this ,you will get result as you want.


Regards

Rahul Sahu

 Hi! Yes, I have tried, but I have problems when the minutes is bigger than 60. There's a way to count as hours when I have this situation?

 

2026-02-26 06-29-24
Rahul
 
MVP

Hi Julia,

Use NewTime () function and use calculation for time like below image

Create a one variable (time data type ) for that hold diff of time- 

NewTime(DiffMinutes(DateTimeVar,DateTimeVar2)/60,Mod(DiffMinutes(DateTimeVar,DateTimeVar2),60), NullIdentifier())


And result is 

See Demo- https://rahul-sahu.outsystemscloud.com/TestEvent/Demo?_ts=637301962338955722 


Hope this will help you.

Regards

Rahul Sahu

UserImage.jpg
Julia Freitas

Rahul Sahu wrote:

Hi Julia,

Use NewTime () function and use calculation for time like below image

Create a one variable (time data type ) for that hold diff of time- 

NewTime(DiffMinutes(DateTimeVar,DateTimeVar2)/60,Mod(DiffMinutes(DateTimeVar,DateTimeVar2),60), NullIdentifier())


And result is 

See Demo- https://rahul-sahu.outsystemscloud.com/TestEvent/Demo?_ts=637301962338955722 


Hope this will help you.

Regards

Rahul Sahu

 Hi! Yes, it helped me. Thank you.


But the problem is with the 0 to 24 format. When I have, for example, to calculate the difference between "02:04" and "19:00", I would like to have "07:04" but I have "00:00:00" (in yellow). In the next line in the image, the result is ok.


 

UserImage.jpg
Julia Freitas

Julia Freitas wrote:

Rahul Sahu wrote:

Hi Julia,

Use NewTime () function and use calculation for time like below image

Create a one variable (time data type ) for that hold diff of time- 

NewTime(DiffMinutes(DateTimeVar,DateTimeVar2)/60,Mod(DiffMinutes(DateTimeVar,DateTimeVar2),60), NullIdentifier())


And result is 

See Demo- https://rahul-sahu.outsystemscloud.com/TestEvent/Demo?_ts=637301962338955722 


Hope this will help you.

Regards

Rahul Sahu

 Hi! Yes, it helped me. Thank you.


But the problem is with the 0 to 24 format. When I have, for example, to calculate the difference between "02:04" and "19:00", I would like to have "07:04" but I have "00:00:00" (in yellow). In the next line in the image, the result is ok.


 

 It's because I'm working only with time, I'm not using date. So, I don't know if it's possible to get the result that I want only with the time information.

 

2026-02-26 06-29-24
Rahul
 
MVP

Julia Freitas wrote:

Julia Freitas wrote:

Rahul Sahu wrote:

Hi Julia,

Use NewTime () function and use calculation for time like below image

Create a one variable (time data type ) for that hold diff of time- 

NewTime(DiffMinutes(DateTimeVar,DateTimeVar2)/60,Mod(DiffMinutes(DateTimeVar,DateTimeVar2),60), NullIdentifier())


And result is 

See Demo- https://rahul-sahu.outsystemscloud.com/TestEvent/Demo?_ts=637301962338955722 


Hope this will help you.

Regards

Rahul Sahu

 Hi! Yes, it helped me. Thank you.


But the problem is with the 0 to 24 format. When I have, for example, to calculate the difference between "02:04" and "19:00", I would like to have "07:04" but I have "00:00:00" (in yellow). In the next line in the image, the result is ok.


 

 It's because I'm working only with time, I'm not using date. So, I don't know if it's possible to get the result that I want only with the time information.

 

 No, It is happening,because it will return negitive value and negitive value not divided thats why you geeting

00:00:00.

Can do one thing check diff is Negitive or positve if diff is positive go as same flow it is working 

and negitive flow first multiply by - sign it will convet as positive value  and do your calution and after calculation is complete show - sign before hours,

see again demo with above link

https://rahul-sahu.outsystemscloud.com/TestEvent/Demo?_ts=637301962338955722


Regards

Rahul Sahu 

2020-05-07 18-53-00
Rui Barradas
 
MVP

Hello Julia,

It is possible, as far as you know the day that you want to compare. In this particular situation, it seems that you want to compare always with the previous day, so you can do it.

First thing, you need to verify if the second time is greater than the first one. If it is, you apply the math that you have right now:

T2 = 20:47

T1 = 17:40

T1 <= T2

Result = T2 - T1 = 20:47 - 17:40 = 03:07


Second, if the second time is smaller (less) than the first one, you need to do the math to compare to that time from the previous day.

T2 = 02:04

T1 = 19:00

T1 > T2

You just need to create a new time with 23:59 value and subtract the T1 value to it.

23:59 - 19:00 = 04:59

Then you add the T2 value to the result, plus 1 minute.

04:59 + 00:01 + 02:04 = 05:00 + 02:04 = 07:04


This will allow you to get the result that you want.


Kind regards,

Rui Barradas

UserImage.jpg
Julia Freitas

Rui Barradas wrote:

Hello Julia,

It is possible, as far as you know the day that you want to compare. In this particular situation, it seems that you want to compare always with the previous day, so you can do it.

First thing, you need to verify if the second time is greater than the first one. If it is, you apply the math that you have right now:

T2 = 20:47

T1 = 17:40

T1 <= T2

Result = T2 - T1 = 20:47 - 17:40 = 03:07


Second, if the second time is smaller (less) than the first one, you need to do the math to compare to that time from the previous day.

T2 = 02:04

T1 = 19:00

T1 > T2

You just need to create a new time with 23:59 value and subtract the T1 value to it.

23:59 - 19:00 = 04:59

Then you add the T2 value to the result, plus 1 minute.

04:59 + 00:01 + 02:04 = 05:00 + 02:04 = 07:04


This will allow you to get the result that you want.


Kind regards,

Rui Barradas

 Hi ! Thank you for your answer. It almost allow me to get the result that I want.


If I have, for example, 

T2 = 18:00

T1 = 19:00

Then, T1 > T2. And if I use the logic of substract 23:59, I will not get 01 hour.


 

2023-08-16 09-51-58
Catarina Gomes

Hello Rui,

How do you actually calculate hours and minutes difference when the datatype is Time or Text?

E.g. 

T2 -> 07:30

T1 -> 00:40

T2 >= T1

So, T2-T1 = 06:50


I understand the logic in practice. But in OutSystems, how can I achieve this?

2020-05-07 18-53-00
Rui Barradas
 
MVP

Hi Julia,

Don't forget that the day also counts. In your scenario it should be something like:

1)

T2 = 19:00 (today)

T1 = 18:00 (today)

Then T1 <= T2, so the result is T2 - T1 = 19:00 - 18:00 = 01:00 hour


2)

T2 = 18:00 (today)

T1 = 19:00 (yesterday)

Then T1 > T2, so the result is 23:59 - 19:00 = 04:59 + 00:01 = 05:00 + 18:00 = 23:00


It always depend on the date.


According to your example, I was assuming that when T1 is greater than T2, than T1 is from yesterday.


Kind regards,

Rui Barradas

UserImage.jpg
Alexandr Ribaruc

I think that we can't make negative Time as class. But we can present it like Text.

You can use this in expression. It's very simple, but before you need calculate difference in seconds (it can be negative because result of function DiffSeconds (t1,t2) can be negative.


  • If(    
  • DiffInSeconds <0    
  • ,   
  •  "-" +  TimeToText(DateTimeToTime(AddSeconds(#00:00:00#,DiffInSeconds*-1)))    
  •  ,    
  • TimeToText(DateTimeToTime(AddSeconds(#00:00:00#,DiffInSeconds))) )

See It in attached image.

Annotation 2022-09-08 150611.jpg
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.