Highlighted
Contributor.
Contributor.
3086 views

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

Jump to solution

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

0 Likes
1 Solution

Accepted Solutions
Highlighted
Contributor.
Contributor.

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

Jump to solution
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.

View solution in original post

0 Likes
6 Replies
Highlighted
Absent Member.
Absent Member.

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

Jump to solution

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

Linden Rowland - IT Consultant
Owner/Developer, www.SchoolReportWriter.com
IT Consultant to Tindle Newspaper Group

0 Likes
Highlighted
Absent Member.
Absent Member.

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

Jump to solution
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
0 Likes
Highlighted
Outstanding Contributor.
Outstanding Contributor.

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

Jump to solution
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.
Highlighted
Contributor.
Contributor.

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

Jump to solution
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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

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

Jump to solution
Great good to hear it worked for you!
Not sure though why you needed to make the mm/dd 4 bytes?
Cheers,
Linden

Linden Rowland - IT Consultant
Owner/Developer, www.SchoolReportWriter.com
IT Consultant to Tindle Newspaper Group

0 Likes
Highlighted
Contributor.
Contributor.

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

Jump to solution
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!
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.