functiondatetimecobol

formatting DATE-TIME IN COBOL


i want to format the FUNCTION DATE-TIME. What i have is

MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-DATA
DISPLAY WS-CURRENT-DATE-DATA

and I tried using a filler

01 WS-CURRENT-DATE-DATA.                               
05 WS-CURRENT-DATE.                                 
   10  WS-CURRENT-YEAR         PIC 9(04).           
   10  FILLER                  PIC X(01)  VALUE "-".
   10  WS-CURRENT-MONTH        PIC 9(02).           
   10  WS-CURRENT-DAY          PIC 9(02).           
05 WS-CURRENT-TIME.                                 
   10  WS-CURRENT-HOURS        PIC 9(02).           
   10  WS-CURRENT-MINUTE       PIC 9(02).           
   10  WS-CURRENT-SECOND       PIC 9(02).           
   10  WS-CURRENT-MILLISECONDS PIC 9(02).

what I get is a result with the hyphen at the back like

2017122818242863- 

but what I want is possibly this

2017-12-28-18:24:28:63  

Pls help. Thanks in advance


Solution

  • The reason that you are seeing this hyphen at then end is because this is actual data definition FUNCTION CURRENT-DATE is expecting:

    05 WS-CURRENT-DATE-TIME.            
       10 WS-CURRENT-DATE.              
          15 WS-CURRENT-YEAR  PIC  9(4).
          15 WS-CURRENT-MONTH PIC  9(2).
          15 WS-CURRENT-DAY   PIC  9(2).
       10 WS-CURRENT-TIME.              
          15 WS-CURRENT-HOUR  PIC  9(2).
          15 WS-CURRENT-MIN   PIC  9(2).
          15 WS-CURRENT-SEC   PIC  9(2).
          15 WS-CURRENT-MS    PIC  9(2).
       10 WS-DIFF-GMT         PIC S9(4).
    

    You will notice that I have an extra field at the end for Greenwich Mean Time Difference. Also, you cannot just add filler to your definition and expect that function to know what to do with it. Basically what is happening is the hyphen that you are putting in (in the picture clause) is being overwritten by the data passed from the function what you need is 2 places in working storage. One to hold the return from the function and one to hold the formatted value, so something like this:

    01 WS-TEMP-DT.   
       05 WS-TEMP-DATE-TIME.            
          10 WS-TEMP-DATE.              
             15 WS-TEMP-YEAR  PIC  9(4). 
             15 WS-TEMP-MONTH PIC  9(2).
             15 WS-TEMP-DAY   PIC  9(2).
          10 WS-TEMP-TIME.              
             15 WS-TEMP-HOUR  PIC  9(2).
             15 WS-TEMP-MIN   PIC  9(2).
             15 WS-TEMP-SEC   PIC  9(2).
             15 WS-TEMP-MS    PIC  9(2).
          10 WS-DIFF-GMT         PIC S9(4).
    
    01 WS-FORMATTED-DT.   
       05 WS-FORMATTED-DATE-TIME.                       
          15 WS-FORMATTED-YEAR  PIC  9(4). 
          15 FILLER             PIC X VALUE '-'.
          15 WS-FORMATTED-MONTH PIC  9(2).
          15 FILLER             PIC X VALUE '-'.
          15 WS-FORMATTED-DAY   PIC  9(2).  
          15 FILLER             PIC X VALUE '-'.           
          15 WS-FORMATTED-HOUR  PIC  9(2).
          15 FILLER             PIC X VALUE ':'.
          15 WS-FORMATTED-MIN   PIC  9(2).
          15 FILLER             PIC X VALUE ':'.
          15 WS-FORMATTED-SEC   PIC  9(2).
          15 FILLER             PIC X VALUE ':'.
          15 WS-FORMATTED-MS    PIC  9(2).
    
    MOVE FUNCTION CURRENT-DATE TO WS-TEMP-DATE-TIME
    MOVE WS-TEMP-YEAR  TO WS-FORMATTED-YEAR
    MOVE WS-TEMP-MONTH TO WS-FORMATTED-MONTH
    MOVE WS-TEMP-DAY   TO WS-FORMATTED-DAY
    MOVE WS-TEMP-HOUR  TO WS-FORMATTED-HOUR
    MOVE WS-TEMP-MIN   TO WS-FORMATTED-MIN
    MOVE WS-TEMP-SEC   TO WS-FORMATTED-SEC
    MOVE WS-TEMP-MS    TO WS-FORMATTED-MS
    
    DISPLAY WS-FORMATTED-DATE-TIME