Date Routine

Does anyone have a simple date routine to subtract hours from a full date time value (YYYYMMDDHHIISS).

Thanks for any help - I think I’m making this way too complicated!

Terry

If you use a time-field, it is very easy:

define data local
1 #time (T)
end-define
#time := *TIMX
write #time (EM=YYYY-MM-DD^HH:II:SS.T)
#time := #time - 2 * 60 * 60 * 10    /* subtract 2 hours
write #time (EM=YYYY-MM-DD^HH:II:SS.T)
end

Thanks Matthias!

It’s easier than that. Forget all that multiplying by 60. Use a time constant (or initialized T-format variable) to add or subtract up to 24 hours, specified in hours, minutes and seconds.

DEFINE DATA LOCAL 
1 #T (T) INIT <TIMX> 
END-DEFINE 
SUBTRACT T'02:30:00' FROM #T  /* 2 hrs 30 min 
DISPLAY *TIMX #T 
END

By the way, this type of question belongs in a “Natural Basics” thread. This thread is where you post code samples, open source, etc. :slight_smile:

If I want to calculate date and time, how can I do. For example, if I have as first date: 2015-01-01 at 23:40:43 and the second date 2015-01-05 at 03:50:10, how can I do the difference between them including date and time.If I use in separate fields, one day could be the difference depending on the time.
I tried to use the format T for both date/time together, but for date, the value is strange: 0000-01-05. if hour2 is greater than hour1, the date is: 0000-01-06.
Does anyone know how this 0000-01-05 can be translated in days.
tks.

Once you understand that a T field is just a number representing tenths of seconds from the time origin (1/1/0000 00:00:00.0) it is not hard to figure out.


DEFINE DATA LOCAL                                                
1 T1     (T)      (EM=MM/DD/YYYY-HH:II:SS.T)                   
1 T2     (T)      (EM=MM/DD/YYYY-HH:II:SS.T)                   
1 TDIFF  (N12)                                                 
1 DAYS   (N12)                                                 
1 HOURS  (N12)                                                 
1 MINS   (N12)                                                 
1 SECS   (N12.1)                                               
1 TNTHS  (N12.1)                                               
END-DEFINE                                                     
*                                                              
COMPUTE T1 = D'01/01/2015' + T'23:40:43'                       
COMPUTE T2 = D'01/05/2015' + T'03:50:10'                       
COMPUTE TDIFF = T2 - T1                                        
DISPLAY T1  T2  TDIFF                                          
DIVIDE 864000 INTO TDIFF GIVING DAYS  REMAINDER TNTHS          
DIVIDE 36000  INTO TNTHS GIVING HOURS REMAINDER TNTHS          
DIVIDE 600    INTO TNTHS GIVING MINS  REMAINDER TNTHS          
DIVIDE 10     INTO TNTHS GIVING SECS  REMAINDER TNTHS          
WRITE / 1X '=' DAYS  / '=' HOURS  / 1X '=' MINS  / 1X '=' SECS 
END                                                            

John,

Take a look at the TMDIFF program in the code sample found here: [url]http://techcommunity.softwareag.com/ecosystem/communities/public/adanat/products/natural/codesamples/5ddd08f1-de89-11e4-89cb-cd8d7ef22065/?title=Date+%26+Time+Calculations[/url]

Thank you Jerome, thank you Ralph.