Python has pretty good date parsing but is the only way to recognize a datetime such as ‘Today 3:20 PM’ or ‘Yesterday 11:06 AM’ by creating a new date today and doing subtractions?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
A library that I like a lot, and I’m seeing more and more people use, is python-dateutil but unfortunately neither it nor the other traditional big datetime parser, mxDateTime from Egenix can parse the word ‘tomorrow’ in spite of both libraries having very strong ‘fuzzy’ parsers.
The only library I’ve seen that can do this is magicdate. Examples:
Unfortunately this only returns datetime.date objects, and so won’t include time parts and can’t handle your example of ‘Today 3:20 PM’.
So, you need mxDateTime for that. Examples:
EDIT: mxDateTime.Parser is only parsing the time in these examples and ignoring the words ‘today’ and ‘tomorrow’. So for this particular case you need to use a combo of magicdate to get the date and mxDateTime to get the time. My recommendation is to just use python-dateutils or mxDateTime and only accept the string formats they can parse.
EDIT 2: As noted in the comments it looks python-dateutil can now handle fuzzy parsing. I’ve also since discovered the parsedatetime module that was developed for use in Chandler and it works with the queries in this question:
and for reference here is the current time: