How to calculate duration between two date/time fields?

I have two date/time fields (both will always be in the past) from which I need to calculate a duration. (i.e. the difference between one and the other.) Ideally, I could display it as hours:minutes, but I'll settle for just hours if I have to.

I tried this:

abs(subtract(number($time_out_c),number($time_in_c)))

but it yields nothing. (a blank field)

I'm a complete new to using calculated fields. Any help would be appreciated.

Parents Reply Children
  • Joseph Moore,

    I recreated this on a demo system this morning and entered 26/01/2014 08:25am as my first date and 24/02/2014 09:25am as my second date, with my formula being "subtract(hoursUntil($date2),hoursUntil($date1))". And this returned me 697.00 hours as expected.

    The only thing I can think of as to why your getting 0 is if I were to do my formula the other way round, so doing "subtract(hoursUntil($date1),hoursUntil($date2))" instead. This is as it would give you a negative number, which depending on how you setup your field may result in it showing as a 0 (for example if you set the minimum value allowed to be 0). In this case you could simply put the "abs" function around the entire formula, which would cause it to always be a positive number.

  • Thanks, Liam.

    I realized that the calculation does work as you describe. My problem was/is that the calculated field is not computed for existing records.

    Is there a way to force Sugar to update all of my existing records? (UPDATE: Sorry, I see that I can update small batches through the list view, but will need to write a script to update all of the records.)

  • In Sugar 7 there is a feature called 'Recalculate Values' which will go through each selected option and update all calculated fields, allowing you to update 1000 records at a time maximum. If you have more than this many records then you can either do this in a few batches, or by exporting the data from your required module and then performing an import and update.