0

Im importing records from a CSV file into a django model. The CSV file is uploaded by the user. The problem I'm facing is with the date field.

The date field expects the date to be in YYYY-MM-DDbut different spreadsheet programs default to different types of formats and I dont want the user to have to change their default format.

So I want to find the best method for django to accept dates that could possibly be in various formats.

What I thought could work is to specify a list of allowed date formats and try and match the string to one of them... thanks

3
  • 2
    It's impossible to detect the format. For example 11/07/2011 means November 7 in some countries and July 11 in others. You can only try some "best guess". Commented Jan 21, 2011 at 1:54
  • Yeah I thought that, which is why I want to allow specified formats. so I could try and match it to one of the formats and run the risk of this being wrong. I'll update the question to be more specific Commented Jan 21, 2011 at 2:02
  • ok I hopefully have updated the question to better explain. Thanks for your help Sven Commented Jan 21, 2011 at 2:10

2 Answers 2

1

The best option I've seen for "fuzy" date string parsing is the dateutil library.

Unfortunately, there are still possible cases where things will be ambiguous and any code will sometimes guess wrong. Dateutil has their matching logic pretty well documented tho, so should you run into wrong guesses you should be able to figure out what happened relatively quickly.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks! this is what I was looking for, plus it gives loads of other functions that will be useful in the future
1

When you convert the date string into a datetime object, you'll likely use datetime.strptime with the format argument. I would ask the user what format they use and/or store it in the database (and by that I mean allow them to choose the format they want).

1 Comment

Thanks Mike, an option that would definitely be "fool proof" and jsut might be the way i'll have to go...although I would like to avoid it if possible

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.