Incrementing Time values

Kladkul

Member
Hey all,

Working with some time values here and need to increment by a predetermined amount of hours. Whats the best way to do this?

Didn't have much luck searching previous posts or on OEH.
 

Kladkul

Member
Found it:

Code:
DEF VAR v-tm AS CHAR. 

ASSIGN v-tm = STRING((TIME + <time in seconds to add>),"HH:MM:SS").
Hope someone else finds this useful.
 

tamhas

ProgressTalk.com Sponsor
Probably didn't find much because no special code is required! All date arithmetic in ABL can be handled by simple addition and subtraction. With newer versions there are even datetime datatypes and some special functions to do fancy things.
 

sdjensen

Member
You could also use the function
Code:
ADD-INTERVAL

Adds a time interval to, or subtracts a time interval from, a DATE, DATETIME, or DATETIME-TZ value, and returns the new value.

Syntax

ADD-INTERVAL (datetime, interval-amount, interval-unit)

datetime
An expression whose value is a DATE, DATETIME, or DATETIME-TZ.
interval-amount
A signed integer (positive or negative) indicating the amount of time you want to add to or subtract from datetime value.
interval-unit
A character constant, or a character expression that evaluates to one of the following time units: ‘years’, ‘months’, ‘weeks’, ‘days’, 'hours’, ‘minutes’, ‘seconds’ or ‘milliseconds’. These values are case insensitive and may be singular.
 

Kladkul

Member
Sdjensen,

I can't seem to get that function to work. I assume interval-amount is the amount of the interval-unit that is declared?

Code:
DEF VAR v-time AS DATETIME. 
 
ASSIGN v-time = ADD-INTERVAL(TODAY,5,'hours').

All that is returned when I display this variable is the date, time is displayed in zeros.
 

sdjensen

Member
TODAY only returns the date, time is not set.
Use NOW instead. ;)
Code:
DEF VAR v-time AS datetime. 
 
ASSIGN v-time = ADD-INTERVAL(NOW,5,'hours').
 

Kladkul

Member
Lol, I just tried that as I got the notification email for this thread :)

Thanks alot!

What is the best way to extract the individual time and date into different variables from the returned value? If I try to get it as a string only the date portion is shown.
 

Casper

ProgressTalk.com Moderator
Staff member
This should do the trick:

Code:
DEF VAR v-time AS datetime. 
DEFINE VARIABLE dDate AS DATE        NO-UNDO.
DEFINE VARIABLE iTime AS INTEGER     NO-UNDO.
DEFINE VARIABLE iTimems AS INTEGER     NO-UNDO.
 
assign v-time  = ADD-INTERVAL(NOW,5,'hours')
       dDate   = date(v-Time)
       iTime   = INTEGER( truncate( MTIME( v-time ) / 1000, 0 ))
       iTimems =  integer(MTIME(v-time) MODULO 1000).
 
MESSAGE 'Date: ' dDate chr(10) 
        'Time: ' string(iTime,'HH:MM:SS') chr(10)
        'Milliseconds: 'string(itimems,'999')
    VIEW-AS ALERT-BOX INFO BUTTONS OK.

Casper.
 
Top