Microsoft KB Archive/50626

Example of Julian Date Function in COBOL

PSS ID Number: Q50626 Article last modified on 04-20-1993

3.00 3.00a | 3.00 3.00a MS-DOS | OS/2

Summary: This article shows how to use reference modification on a value returned from the DATE function to create a Julian day. A Julian day is a number that indicates the sum of the days for any date since an arbitrary starting point. In the program below, the Julian day 1 starts on January 1, 1583. The COBOL 3.0 or 3.0a DATE function returns a value in the form of YYMMDD, which is useful for many instances. However, there are cases when a Julian day is needed. The Julian day is not a built-in function in Microsoft COBOL 1.x, 2.x, 3.0, or 3.0a.

More Information: Since this article shows how to create a portable Julian day, it loads the table with several MOVE statements. This code can be replaced by using a file to load the table. Further, often there is a need to determine the amount of days necessary to complete a job given the job’s starting and ending dates. The code illustrated below can be modified to achieve this. To do so would require the following steps (capitalized words indicate variable names): 1. Calculate the Julian day for the STARTING-DATE using the code segment below. 2. Calculate the Julian day for the ENDING-DATE using the code segment below. 3. Set TOTAL-JULIAN-DAYS equal to the ENDING-DATE less the STARTING-DATE. The code example below shows how to derive a Julian day from the current day through use of the DATE function.

Code Example
$SET ANS85 $SET NOOSVS IDENTIFICATION DIVISION. PROGRAM-ID. JULIAN-DATE. ****************************************************** *  *  THIS PURPOSE OF THIS PROGRAM IS TO SHOW A METHOD OF  *  DERIVING THE JULIAN DAY FOR THE CURRENT DAY USING * THE COBOL DATE FUNCTION. * ******************************************************   WORKING-STORAGE SECTION. * *                     CONSTANTS 78 century       VALUE 1900. * *                     INDIVIDUAL VARIABLES 77 stall         PIC X.   77 today          PIC 9(6). 77 theday        PIC 99. 77 themonth      PIC 99. 77 theyear       PIC 9999. 77 checkyear     PIC v999. 77 yearcnt       PIC 9999. 77 juliandays    PIC 9(10)      VALUE IS 0. 77 editjuliandays PIC Z,ZZZ,ZZZ,ZZZ. *  01 DATE-TABLE. 05 LeapOrStd OCCURS 2 TIMES. 10 sums    OCCURS 12 TIMES   PIC 999. * *                     ROWS ARE SUMMED DAYS FROM MONTHS *                    COLUMN 1 IS THE LEAP YEAR *                    COLUMN 2 IS THE STANDARD YEAR *  PROCEDURE DIVISION. 000-main. perform 100-load-table. perform 200-get-date. perform 300-calculate-julian. move juliandays to editjuliandays. display &quot;The julian day is &quot; editjuliandays. display &quot;Hit Return&quot;. accept stall. stop run. *  100-load-table. *        BETTER CODING WOULD LOAD VALUES FROM A FILE *        INSTEAD OF USING MOVE: move 0 to sums (1,1). move 0 to sums (2,1). move 31 to sums (1,2). move 31 to sums (2, 2). move 60 to sums (1,3). move 59 to sums (2,3). move 91 to sums (1,4). move 90 to sums (2,4). move 121 to sums (1,5). move 120 to sums (2,5). move 152 to sums (1,6). move 151 to sums (2,6). move 182 to sums (1,7). move 181 to sums (2,7). move 213 to sums (1,8). move 212 to sums (2,8). move 244 to sums (1,9). move 243 to sums (2,9). move 274 to sums (1,10). move 273 to sums (2,10). move 305 to sums (1,11). move 304 to sums (2,11). move 335 to sums (1,12). move 334 to sums (2,12). *  200-get-date. accept today from date. move today (5:2) to theday. move today (3:2) to themonth. move today (1:2) to theyear. compute theyear = century + theyear. *  300-calculate-julian. *                    CHECK FOR LEAP YEAR FIRST compute checkyear = theyear / 4. if checkyear = 0 *                    THIS IS A LEAP YEAR move sums (1,themonth) to juliandays else move sums(2,themonth) to juliandays. *                    ADD THE OTHER YEARS TO THE DAYS *                    IN THIS YEAR compute juliandays = juliandays + theday. compute theyear = theyear - 1. perform 400-sum-juliandays varying yearcnt from theyear by -1 until yearcnt < 1583. *  400-sum-juliandays. compute checkyear = yearcnt / 4. if checkyear = 0 compute juliandays = juliandays + 366 else compute juliandays = juliandays + 365. Additional reference words: 3.00 3.00a Copyright Microsoft Corporation 1993.