I needed code to allow users to enter dates in American format MM/DD/YYYY, and for it to save correctly in my database. Previously, I was using American Date Monkey Patch code (http://stackoverflow.com/questions/6825048/how-to-make-assigning-an-american-formatted-date-string-into-a-date-variable-wor), but I started having issues with it.
So I moved to this gem by Jeremy Evans that does the same, The Ruby American Date Gem:
I’ve been working on building dummy data into my local database. Uploading a CSV that I filled with data using Excel, I populated my database. Here are two lessons I learned.
For one of the fields that I didn’t populate with data, imported a 0 because it was an integer field. However, I needed the field null. When I went into the record and took out the 0 and tried to save it, I got an error. What I found was that when I clicked the checkbox for null for the record, it nullified the field.
The date format was the European format in my database YYYY-MM-DD. However, when I had the dates in the American format in my CSV MM-DD-YYYY. The records imported successfully in my database, but instead of erring out with the data, it set the date to 0000-00-00. So I just had to delete the imported records, and then edit the date format in the CSV and re-import.
My dates were displaying DD-MM-YYYY, but I wanted to display it MM/DD/YYYY.
Here’s an easy way to do that.