Time calculation

AsallaiAsallai Member Posts: 141
Hi All,

It is possible to calculate Time variable? I mean I'd like to increment a Time field with a variable of Time Type. :?
I've got an error message when I tried to add 2 Time variable. But in this case how can I cumulative two Time field with each other? :|
Thank you!

Comments

  • EugeneEugene Member Posts: 309
    one cannot add two dates because it is meaningless. For example what would it mean to add January 1st 2007 to May 20th 2005 ?
    One can only add interval to a date, or subtract an interval from a date.
    To get an interval you need to subtract one date from another or specify the interval implicitly in CALCDATE function.
  • kinekine Member Posts: 12,562
    If you want to do something like 5:00:00 + 3:00:00 = 8:00:00 you can use the DateTime type for that. You can transform the time to DateTime with 1.1.0000 date part and add them. Than extract the time part from the DateTime. Do not forget that if you do "23:00:00" + "2:00:00" it will be next day with time "1:00:00".

    It means:
     NewTime := DT2TIME(CREATEDATETIME(0D,MyTime1)+(CREATEDATETIME(0D,MyTime2)-CREATEDATETIME(0D,0T)))
    
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • AsallaiAsallai Member Posts: 141
    Thanks a lot kine, I've searched this :)

    Eugene, thanks you too. :wink:
  • AlkroAlkro Member Posts: 115
    Why this sentence shows an error??:

    "Type conversion is not possible because 1 of the operators contains an invalid type"
    VarDatetime := CREATEDATETIME(Capacity."Ending Date",Capacity."Ending Time") + CREATEDATETIME(0D,030000T);
                                // Add 3 Hours
    
  • kinekine Member Posts: 12,562
    Because you need to add just duration, not another datetime. In the example there is DateTime+(DateTime-DateTime) which means DateTime+Duration. It is why there is the part "-CREATEDATETIME(0D,0T)" - to convert DateTime to duration.. ;-) it is a little tricky...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • DevendraSharmaDevendraSharma Member Posts: 23
    My suggestion is simple. Use variable of type Duration (represent the difference between two datetimes, in milliseconds.) to calculate time.

    // 03:20 = (3 * 60 minutes) + 20 minutes = 200 minutes
    // 200 minutes = 200 * 60 seconds = 12 000 seconds
    // 12 000 seconds = 12 000 * 1000 milliseconds = 12 000 000 milliseconds

    varInteger := (3 * 60 + 20) * 60 * 1000; // desired time specified in milliseconds.
    varDuration := var varInteger;
    varTime := 0000T + varDuration;

    MESSAGE(FORMAT(varTime)); // result is 03:20:00

    So simply add some duration period in milliseconds to zero time and calculating a new time.


    Thank's !!!

    Devendra Sharma
Sign In or Register to comment.