Overview
Handling date and time functions in COBOL is crucial for applications that require date calculations, aging reports, scheduling, and more. Given COBOL's extensive use in financial and government sectors, where precise date and time processing is essential, mastering these functions is key for developers.
Key Concepts
- Intrinsic Functions for Date and Time: COBOL offers a set of intrinsic functions like
CURRENT-DATE
, which returns the current date and time. - Date and Time Formats: Understanding different date formats (e.g., YYYYMMDD) and their manipulation.
- Date Arithmetic: Performing calculations with dates, such as adding days to a date or calculating the difference between two dates.
Common Interview Questions
Basic Level
- How do you retrieve the current date and time in COBOL?
- How can you calculate the age of a person in years based on their date of birth in COBOL?
Intermediate Level
- Describe how you would add a specific number of days to a given date in COBOL.
Advanced Level
- Discuss how you would handle leap years when performing date arithmetic in COBOL.
Detailed Answers
1. How do you retrieve the current date and time in COBOL?
Answer: In COBOL, you can retrieve the current date and time using the CURRENT-DATE
intrinsic function. This function returns a 21-character alphanumeric field in the format of YYYYMMDDHHMMSSSSSSSS, where YYYY is the year, MM is the month, DD is the day, HH is the hour, MM is the minute, SS is the second, and SSSSSS are the microseconds.
Key Points:
- CURRENT-DATE
returns both date and time.
- The result is in a compact alphanumeric format.
- You may need to parse the result for specific date or time components.
Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. DateExample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CURRENT-DATE-AND-TIME PIC X(21) VALUE CURRENT-DATE.
01 YEAR PIC 9(4).
01 MONTH PIC 9(2).
01 DAY PIC 9(2).
PROCEDURE DIVISION.
DISPLAY "Current Date and Time: " CURRENT-DATE-AND-TIME
UNSTRING CURRENT-DATE-AND-TIME(1:8)
INTO YEAR, MONTH, DAY
DISPLAY "Current Year: " YEAR
DISPLAY "Current Month: " MONTH
DISPLAY "Current Day: " DAY
STOP RUN.
2. How can you calculate the age of a person in years based on their date of birth in COBOL?
Answer: Calculating the age of a person requires obtaining the current date, extracting the year, and then subtracting the birth year from the current year. Adjustments are made if the current date is before the person's birthday in the current year.
Key Points:
- Use CURRENT-DATE
for the current date.
- Perform arithmetic operations for year subtraction.
- Adjust for the birth date not yet reached in the current year.
Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. AgeCalculation.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CURRENT-DATE-AND-TIME PIC X(21) VALUE CURRENT-DATE.
01 CURRENT-YEAR PIC 9(4).
01 BIRTH-YEAR PIC 9(4) VALUE 1990.
01 AGE PIC 9(3).
PROCEDURE DIVISION.
MOVE FUNCTION NUMVAL(CURRENT-DATE-AND-TIME(1:4)) TO CURRENT-YEAR
COMPUTE AGE = CURRENT-YEAR - BIRTH-YEAR
DISPLAY "Age is: " AGE
STOP RUN.
3. Describe how you would add a specific number of days to a given date in COBOL.
Answer: Adding a specific number of days to a date in COBOL involves using the DATE-OF-INTEGER
and INTEGER-OF-DATE
functions for date manipulation. Convert the date to an integer, add the number of days, and then convert it back to date format.
Key Points:
- Convert date to integer using INTEGER-OF-DATE
.
- Perform the addition of days.
- Convert back to a date using DATE-OF-INTEGER
.
Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. DateAddition.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMERIC-DATE PIC 9(8).
01 DATE-TO-ADD PIC 9(8) VALUE 20230101.
01 DAYS-TO-ADD PIC 9(5) VALUE 30.
01 RESULT-DATE PIC 9(8).
PROCEDURE DIVISION.
MOVE FUNCTION INTEGER-OF-DATE(DATE-TO-ADD) TO NUMERIC-DATE
ADD DAYS-TO-ADD TO NUMERIC-DATE
MOVE FUNCTION DATE-OF-INTEGER(NUMERIC-DATE) TO RESULT-DATE
DISPLAY "New Date: " RESULT-DATE
STOP RUN.
4. Discuss how you would handle leap years when performing date arithmetic in COBOL.
Answer: When performing date arithmetic involving leap years, you must account for the extra day in February. This involves checking if the year is divisible by 4 but not by 100, unless it's also divisible by 400.
Key Points:
- Leap years are every 4 years but not every century unless divisible by 400.
- Use conditional logic to adjust for the extra day in leap years.
- Consider leap year rules in date calculations to ensure accuracy.
Example:
IDENTIFICATION DIVISION.
PROGRAM-ID. LeapYearCheck.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 YEAR PIC 9(4) VALUE 2024.
01 IS-LEAP-YEAR PIC X VALUE 'N'.
88 LEAP-YEAR VALUE 'Y'.
88 NOT-LEAP-YEAR VALUE 'N'.
PROCEDURE DIVISION.
IF YEAR MOD 4 = 0 THEN
IF YEAR MOD 100 = 0 THEN
IF YEAR MOD 400 = 0 THEN
SET LEAP-YEAR TO TRUE
ELSE
SET NOT-LEAP-YEAR TO TRUE
ELSE
SET LEAP-YEAR TO TRUE
ELSE
SET NOT-LEAP-YEAR TO TRUE.
IF LEAP-YEAR THEN
DISPLAY YEAR " is a leap year."
ELSE
DISPLAY YEAR " is not a leap year."
STOP RUN.