Showing posts with label Gregorian Date. Show all posts
Showing posts with label Gregorian Date. Show all posts

Get the Last Date of the Month

Using COBOL intrinsic functions, we can find the last Date of the Month for any given Gregorian Date.




















The below code is present in the above screenshot.

01 WS-MONTH-END-DD             PIC X(24) VALUE              
                               '312831303130313130313031'. 
01 WS-TBL-MONTH-END REDEFINES WS-MONTH-END-DD.              
    05 TBL-MONTH-END-DAY       PIC 9(02) OCCURS 12 TIMES.  

01 WS-GREG-DATE. 
   05 WS-GREG-YEAR             PIC 9(04). 
   05 WS-GREG-MNTH             PIC 9(02). 
   05 WS-GREG-DAY              PIC 9(02). 

EVALUATE TRUE                                      
    WHEN FUNCTION MOD (WS-GREG-YEAR 4)   NOT ZERO    
    WHEN FUNCTION MOD (WS-GREG-YEAR 100)     ZERO      
     AND FUNCTION MOD (WS-GREG-YEAR 400) NOT ZERO 
    MOVE '28' TO WS-TBL-MONTH-END (3: 2)          
WHEN OTHER                                        
    MOVE '29' TO WS-TBL-MONTH-END (3: 2)          
END-EVALUATE                                      
                                                  
MOVE TBL-MONTH-END-DAY(WS-GREG-MNTH)              
                           TO WS-GREG-DAY 
        
DISPLAY 'LAST-DATE OF MONTH:' WS-GREG-DATE

Subtract Days from a Gregorian Date

We can easily subtract number of Days from a given Gregorian Date to get a past Date using COBOL intrinsic functions.








The below code is present in the above screenshot.

01 WS-GREGORIAN-DATE           PIC 9(08). 
01 WS-SUB-DAYS                 PIC 9(08). 
01 WS-PAST-DATE                PIC 9(08). 

     COMPUTE WS-PAST-DATE  = FUNCTION DATE-OF-INTEGER            
       (FUNCTION INTEGER-OF-DATE(WS-GREGORIAN-DATE) - WS-SUB-DAYS)

Add Days to a given Gregorian Date

We can easily add number of Days to a given Gregorian Date to get a future Date using COBOL intrinsic functions.








The below code is present in the above screenshot.

01 WS-FUTURE-DATE              PIC 9(08). 
01 WS-ADD-DAYS                 PIC 9(08). 
01 WS-GREGORIAN-DATE           PIC 9(08). 

COMPUTE WS-FUTURE-DATE = FUNCTION DATE-OF-INTEGER            
  (FUNCTION INTEGER-OF-DATE(WS-GREGORIAN-DATE) + WS-ADD-DAYS)

Convert Gregorian Date to Julian Date and vice-versa

We often need to convert date from one format to another. This can be done using COBOL intrinsic functions.











The below code is present in the above screenshot.

01 WS-GREGORIAN-DATE           PIC 9(08).    
01 WS-JULIAN-DATE              PIC 9(07).    

COMPUTE WS-JULIAN-DATE    = FUNCTION DAY-OF-INTEGER      
                            (FUNCTION INTEGER-OF-DATE    
                            (WS-GREGORIAN-DATE))        
                                                                                                              
COMPUTE WS-GREGORIAN-DATE = FUNCTION DATE-OF-INTEGER 
                            (FUNCTION INTEGER-OF-DAY 
                            (WS-JULIAN-DATE))