Skip to content

Instantly share code, notes, and snippets.

@MMohan1
Last active September 12, 2018 13:43
Show Gist options
  • Save MMohan1/b6155e41d41c63ec6473d1815043cf23 to your computer and use it in GitHub Desktop.
Save MMohan1/b6155e41d41c63ec6473d1815043cf23 to your computer and use it in GitHub Desktop.
Python date time python-dateutil package issue
Q - HOW TO GET THE DATETIME PYTHON OBJ FROM A DATE STRING WITHOUT KNOWING THE STRING DATETIME FORMAT.
A- we use "delorean" (pip install delorean) package to make that string in to python datetime
object. How to use it --
pip install delorean
Go to python shell and try this
>>> datetime_str = "2017-07-17"
>>> from delorean import parse
>>> parse(datetime_str.strip()).datetime.month
7
>>> parse(datetime_str.strip()).datetime.day
17
so you will get the datetime obj from the string.
Note - Here the package will identify what is the month,day and year from the given string.
Issues-
delorean uses python package python-dateutil.
While we install delorean it automatically install python-dateutil latest package that is 0.6.0 .
>>> datetime_str = "2017-06-07"
>>> parse(datetime_str.strip()).datetime.month
7
>>> parse(datetime_str.strip()).datetime.day
6
While this package try to identify the month by default it will take the last identified number from the string as MONTH and second last as DAY in string order.So if you have string 2017-06-07
it will give month 7 and day 6.If your format is YYYY-MM-DD then this is a problem.
But in package version 0.4.1 it was vise versa and it will give you correct results. For the given string the day will be 7 and month will be 6
>>> datetime_str = "2017-06-07"
>>> parse(datetime_str.strip()).datetime.month
6
>>> parse(datetime_str.strip()).datetime.day
7
I am not going to tell this is a bug or issue but it is the change in behavior of package upgrade.
To resolve this issue there is to step i think-
1. Downgrade the package version to 0.4.1 and reinstall it after intalling the "delorean".
2. Second thing we have to change the string format according new version.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment