Time Formatting Parsing in COBOL
Our first example demonstrates time formatting and parsing in COBOL. Here’s the full source code:
In this COBOL program, we demonstrate basic time formatting and parsing operations:
We use the
FUNCTION CURRENT-DATE
to get the current date and time.We then format this date into a string similar to the RFC3339 format using the
STRING
verb.For parsing, we use a predefined date string and parse it using the
UNSTRING
verb, which splits the string into its component parts.Both the formatted and parsed dates are then displayed.
COBOL doesn’t have built-in functions for complex date formatting and parsing like some modern languages. However, we can achieve similar results using string manipulation operations.
To run this program:
- Save the code in a file with a
.cob
extension (e.g.,time-formatting-parsing.cob
). - Compile the program using a COBOL compiler.
- Run the compiled program.
The output will show the current date formatted in a similar style to RFC3339, and then show the parsed components of the predefined date string.
Note that COBOL’s date and time handling capabilities can vary depending on the specific COBOL compiler and version you’re using. Some modern COBOL environments may provide additional intrinsic functions for more advanced date and time operations.