Date Conversion

September 3, 2019

Where I work, the accounting department does a survey of its vendors once a year, or every three years, or something, I’m not sure of the details. All of the vendors send CSV files with multiple rows, from a few rows to several hundred; each row contains 3 dates. Different vendors send dates in different formats: 9/3/19, 9-3-19, 09/03/2019, 3-SEP-19, “September 3, 2019” (quoted to protect the embedded comma), and even 09/03/2019T14:37:26.8493 (because every ten-thousandth of a second matters). That variety of formats caused problems for the accounting department, so they asked for help in reformatting the dates. Here is some sample data:

John,Smith,9/3/19,9-3-19,09/03/19,100
Sally,Jackson,3-SEP-19,"September 3, 2019",09/03/2019T14:37:26.8493,200

Your task is to write a program to convert dates from a variety of formats to the standard MM/DD/YYYY format. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.

Advertisement

Pages: 1 2

4 Responses to “Date Conversion”

  1. John Cowan said

    All of which is fine until someone sends you a date like “December 7th, 1941” or “die achte Mai, 1945”. Fortunately, there’s a Python library “dateparser” that knows about many such unusual date formats. You could translate it into Scheme if you had to.

  2. programmingpraxis said

    @JohnCowan: The real version of my program actually handled dates like December 7th, 1941 (but not your German date). I also had a few date formats that were so infrequently seen that I didn’t bother to write a conversion; instead, I edited the file manually. This was a quick-and-dirty program, I spent less than an afternoon on the entire project, and my primary goal was to get it done — pretty didn’t count. It does make a good exercise in sed, however.

  3. The read link does not work

  4. programmingpraxis said

    @bookofstevegraham: Fixed. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: