Need to convert mm/dd/yyyy or m/dd/yyyy or m/d/yyyy into YYYYMMDD

I'm reading a .csv file in COBOL and writing a fixed-length .txt file.   I want all the dates in the .txt file to be YYYYMMDD format.

The .csv has numerous dates, all formatted as month/day/year.  I've tried UNSTRING, which works when mm and dd are both two digits (e.g.,12/17/1996) but it doesn't work with 1-digit months or days, like 1/17/1996 or 12/7/1996.

I could struggle through a character-handling routine, but I'm hoping somebody out there has an easier solution.

 

My Micro Focus product specs are as follows.

Microsoft Visual Studio 2010
Version 10.0.40219.1 SP1Rel
Microsoft .NET Framework
Version 4.6.01586 SP1Rel

Installed Version: IDE Standard

Microsoft Visual Web Developer 2010 01011-532-2002361-70367
Microsoft Visual Web Developer 2010

  • Hi, I'd use unstring and function numval. Something like:

    01 outDate.
     03 outYYYY pic x(4).
     03 filler pic x value '/'.
     03 outMM pic 99.
     03 filler pic x value '/'.
     03 outDD pic 99
    01 workDD pic xx.
    01 workMM pic xx.

    initialize workMM, workDD.
    unstring inDate delimited by '/' into workMM, workDD, outYYYY.
    move function numval(workDD) to outDD
    move function numval(workMM) to outMM

    Haven't tested it!

    Regards,
    Linden

  • You can use the DateTime class....
    Eg ( C# ):

    DateTime dt;
    if(!DateTime.TryParseExact("28/02/2017", "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
    Console.WriteLine("Fail... :( ");

    Console.WriteLine("{0}", dt.ToString("yyyyMMdd")); // This will print 20170228
  • Equivalent in Cobol:

    declare inDate as type DateTime
    if type DateTime::TryParse("3/31/2017", type System.Globalization.CultureInfo::InvariantCulture, type System.Globalization.DateTimeStyles::None, output inDate)
    display "Datum " inDate::ToLongDateString & " --> " & inDate::ToString("yyyyMMdd")
    else display "Datum ungültig"
    end-if.
  • Verified Answer

    With some tweaking (making receiving fields wider) your solution worked! Thanks a trillion. Here's what I did:

    * Fields for changing mm/dd/yyyy input into yyyymmdd output
    01 W-RAW-DATE PIC X(10).
    01 W-IN-DATE.
    05 W-IN-MM PIC X(04).
    05 W-IN-DD PIC X(04).
    05 W-IN-YYYY PIC X(05).
    01 W-OUT-DATE.
    05 W-OUT-YYYY PIC 9(04).
    05 W-OUT-MM PIC 9(02).
    05 W-OUT-DD PIC 9(02).
    .
    .
    MOVE IN-DATE-SENTENCED TO W-RAW-DATE.
    PERFORM 3000-CONVERT-DATE.
    MOVE W-OUT-DATE TO M-DATE-SENTENCED.
    .
    .
    3000-CONVERT-DATE SECTION.
    3000-ENTRY.
    UNSTRING W-RAW-DATE
    DELIMITED BY '/'
    INTO W-IN-MM
    W-IN-DD
    W-IN-YYYY.
    MOVE FUNCTION NUMVAL (W-IN-YYYY) TO W-OUT-YYYY.
    MOVE FUNCTION NUMVAL (W-IN-MM) TO W-OUT-MM.
    MOVE FUNCTION NUMVAL (W-IN-DD) TO W-OUT-DD.
    3000-EXIT. EXIT.
  • Great good to hear it worked for you!
    Not sure though why you needed to make the mm/dd 4 bytes?
    Cheers,
    Linden
  • It didn't unstring correctly with 2 bytes for mm/dd. Did better, but not perfect, with 3 bytes. Did perfect with 4 bytes. As for the YYYY, it didn't do right with 4 bytes, but did perfect with 5 bytes. Don't ask me why. Twilight Zone!