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

It finds the integer value of a given date. It is same as above but the input format is different here.

FUNCTION INTEGER-OF-DAY(date in yyyyddd).
YYYY is the Year & DDD is the day number in that year which should be less than 367 & should be valid for that year.

For example 2019 Jan 1st would be represented as 2019001. The out would be the number of days starting from 31st December 1600.

Example:

MOVE 16010102 TO WW-DTE

COMPUTE WW-INT-DTE = FUNCTION INTEGER-OF-DAY(WW-DTE)

Output would be 2 as it calculates the number of days difference between the input date and 31st Dec 1600.

7. DAY-OF-INTEGER

It is the reverse of INTEGER-OF-DAY where it converts the number into the corresponding date of the form YYYYDDD.

Example:

COMPUTE WK-DTE = FUNCTION INTEGER-OF-DAY(WW-INT-DTE)

here WW-INT-DTE value is 2. so the out of this function is 16010102

8. DATE-TO-YYYYMMDD

It adds century to the date in the form YYMMDD

9. YEAR-TO-YYYY

It converts the two digit year into 4 digit year by adding century.

Add a Comment

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