Difference between duration time

sharon95
sharon95 Member Posts: 183
In a report I have to calculate the sum of differences between the duration of two elapsed times...

Example:
Time_1 From: 09:00 Time_1 To: 10:00 = elapsed: 01:00:00
Time_2 From: 13:00 Time_2 To: 14:00 = elapsed: 01:00:00
____________________
Sum: 02:00:00

How can I have the Sum? When I try it gives me #Error ...

Thanks!

Answers

  • sharon95
    sharon95 Member Posts: 183
    I tried with FormatDateTime(Sum(Fields!TimeToG.Value - Fields!TimeFromG.Value),DateFormat.LongTime) but it doesn't work...
  • logger
    logger Member Posts: 126
    Hi @sharon95 ,
    I can be obvious, but what if to calculate this difference in NAV code and then send calculated value to the report?
    Let's go!
  • sharon95
    sharon95 Member Posts: 183
    edited 2017-12-18
    logger wrote: »
    Hi @sharon95 ,
    I can be obvious, but what if to calculate this difference in NAV code and then send calculated value to the report?

    It always gives me an error....
  • logger
    logger Member Posts: 126
    @sharon95 ,
    Could you attach a screenshot of error message?
    Let's go!
  • sharon95
    sharon95 Member Posts: 183
    logger wrote: »
    @sharon95 ,
    Could you attach a screenshot of error message?

    simply "#Error" in the report field
  • logger
    logger Member Posts: 126
    @sharon95 , even if you calculate the difference in code? How do you do it?
    Let's go!
  • sharon95
    sharon95 Member Posts: 183
    logger wrote: »
    @sharon95 , even if you calculate the difference in code? How do you do it?

    in code I get type conversion error even if all three variables are Time.
    I'm trying ElapsedTime := TimeTo - TimeFrom; ...
  • logger
    logger Member Posts: 126
    @sharon95 ,
    Please, attach a screenshot of type conversion error.
    Let's go!
  • sharon95
    sharon95 Member Posts: 183
    Type conversion is not possible because 1 of the operators contains an invalid type. Time := Integer
  • sharon95
    sharon95 Member Posts: 183
    if I write Sum(Fields!TimeToG.Value - Fields!TimeFromG.Value) in the report I get the milliseconds.... any help?
  • logger
    logger Member Posts: 126
    @sharon95 ,
    You can just multiply it by 1000 to get seconds.
    Let's go!
  • sharon95
    sharon95 Member Posts: 183
    logger wrote: »
    @sharon95 ,
    You can just multiply it by 1000 to get seconds.

    :'( :'( :'( it seems it doesn't work
  • logger
    logger Member Posts: 126
    @sharon95 ,
    Why do you think so? What was the value os milliseconds and what you get multiplying by 1000?
    Let's go!
  • Slawek_Guzek
    Slawek_Guzek Member Posts: 1,692
    edited 2017-12-18
    The Duration type is stored/calculated as a number of milliseconds.

    It can be assigned to/from an integer, or BigInteger vars. If you need a number of seconds you need to divide the duration by 1000, not to multiply it by 1000.

    Try this:
    ElapsedTimeMilisecondsBigInt := TimeTo - TimeFrom;
    ElapsedTimeSecsondsInt := ROUND( ElapsedTimeMilisecondsBigInt/ 1000.0, 1);
    ElapsedTimeDuration  := ElapsedTimeSecsondsInt * 1000; //back to miliseconds
    ElapsedTimeDurationTxt := FORMAT(ElapsedTimeDuration);
    
    Pass both ElapsedTimeSecsondsInt and ElapsedTimeDurationTxt vars in your data item, then use the text version to display the individual lines duration and ElapsedTimeSecsondsInt to add up the number of seconds.

    The ElapsedTimeDuration var is of Duration type, and is used in the example to get proper formatting.

    Using of BigInteger in ElapsedTimeMilisecondsBigInt in this case is non-essential as you are only dealing with Time variables, but it is good to get using it as your general habit when dealing with duration, as when you start comparing/formatting datetimes the duration can go out of allowed integer values range easily.
    Slawek Guzek - www.yitron.co.uk
    Business Central, MS SQL Server, Wherescape RED;