Adding time to time

HenningThHenningTh Member Posts: 59
Hi all,
I am not a novice in coding C/AL - but this one really have teased me, and have not yet found the solution despite searching the net for some good comments. The trouble:

Have a field (type TIME) holding the time - say 22:30:00

I have an integer-value of 8 and want to add this as 8 hours to the field and if it passes midnight (which it does in this case), I will have to increase a DATE-field by CALCDATE adding 1D.

Anyone having an input to this (to me) annoying problem?

Thanks in advance.

Comments

  • David_SingletonDavid_Singleton Member Posts: 5,479
    convert to date time and add 8 hours to that then convert back. Much easier that way.
    David Singleton
  • MBergerMBerger Member Posts: 413
    You can add the number of hours in milliseconds to a datetime, and then see if the date changes. (Maybe there is a better way, but i found that this works after a bit of fiddling around. )


    D : Date ;
    T : Time ;
    DT :datetime ;
    HoursToAdd : Integer ;
    TimeValue : Time ( your value )
    D := 19000101D ;
    T := TimeValue ;
    
    DT := CreateDateTime(D,T) ;
    
    if DT2Date(DT + (HoursToAdd * 60 * 60 * 1000)) > D then
      Message('Midnight passed')
    else
      Message('Midnight not passed') ;
    
Sign In or Register to comment.