COBOL date functions list, add find Duration

Below are COBOL date functions to add, Subtract and Find the duration between Dates, Times and Time stamps.

1. ADD-DURATION

This is rarely seen in our programs but really a helpful Intrinsic function that COBOL provided.

This is to add a specific duration to a date/time variable/value. For example if 1 year needs to be added to a specific timestamp, then this function can be used very well instead of calculating it pragmatically.

Ex:
MOVE FUNCTION ADD-DURATION (WW-TIMESTMP-1 YEARS 1) TO WS-TIMESTMP-2.

Adding years and months to a specific Timestamp

MOVE FUNCTION ADD-DURATION (WW-TIMESTMP-1 YEARS 2 MONTHS 5) TO WS-TIMESTMP-2.

Adding years,months AND DAYS to a specific Timestamp

MOVE FUNCTION ADD-DURATION (WW-TIMESTMP-1 YEARS 2 MONTHS 5 DAYS 23) TO WS-TIMESTMP-2.

Like above this function allows to add HOURS,MINUTES,SECONDS and MICROSECONDS as well to a Timestamp.

This can be used for a date or time or timestamp variables

General Syntax:
>>-FUNCTION ADD-DURATION–(–argument-1—-argument-2–argument-3-+–)-><

argument-2 and 3 can appear multiple times as shown in the example below

MOVE FUNCTION ADD-DURATION (WW-TIMESTMP-1 YEARS 2 MONTHS 5 DAYS 23) TO WS-TIMESTMP-2.

2. SUBTRACT-DURATION

It works in the same manner as ADD_DURATION function where as it subtracts the duration given.

3. FIND-DURATION

It is used to calculate a duration between dates/timestamps or times. It can be used to find a duration between a Date and a Timestamp, A time and a Timestamp along with Two dates, Two times and two Timestamps.

4. CURRENT-DATE

Used to find the current value of date.

Example:

Working Storage
01 WW-CURR-DATE PIC X(21) VALUE SPACES.

PROCEDURE DIVISION.

MOVE FUNCTION CURRENT-DATE TO WW-CURR-DATE
DISPLAY ‘WW-CURR DATE–> ‘WW-CURR-DATE

Output:
WW-CURR DATE–> 2019010902463469-0659
It displays the 21 char date value in the format of CCYYMMDDHHMMSSmm<time differential from Greenwich>

5. INTEGER-OF-DATE

It gives the date value in Integer format by converting it from Gregorian dates(YYYMMDD). Returned integer value is the number of dats from Jan 01, 1601. It takes 16010101 as its base date for calculating.

Example:

01 WW-CURR-DATE-1 PIC 9(08) VALUE 16010102.
01 WW-INT-DATE PIC 9(08) VALUE ZEROES.

COMPUTE WW-INT-DATE =
FUNCTION INTEGER-OF-DATE(WW-CURR-DATE-1)
DISPLAY ‘INT DATE–>’WW-INT-DATE

Output :

INT DATE–>00000002

6. INTEGER-OF-DAY
7. DAY-OF-INTEGER
8. DATE-TO-YYYYMMDD
9. YEAR-TO-YYYY

Leave a Reply

Your email address will not be published. Required fields are marked *

Bitnami