54

Python 2.x has a great function called dateutil.parser which turns an ISO8601 formatted date into a python datetime value. It's not present in Python 3. What is the replacement?

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
vy32
  • 28,461
  • 37
  • 122
  • 246

4 Answers4

89

dateutil is a third party module. It has recently been ported to Python 3 with dateutil 2.0, and the parser functions was ported as well.

So the replacement is dateutil.parser. You just forgot to install it.

Gowtham
  • 11,853
  • 12
  • 43
  • 64
Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
14

You should first find the exact name for the module using pip search:

pip search dateutil

Then, install the version you want (assuming py-dateutil):

pip install py-dateutil

Now, fire-up shell and import the module (pitfall: the module is not called py-dateutil):

import dateutil.parser

You should be good to go!

EarlyCoder
  • 1,213
  • 2
  • 18
  • 42
2

You can achieve this through the datetime module's strptime method.

>>> import datetime
>>> mydate = datetime.datetime(2002,12,4, 12, 30).isoformat()
>>> mydate
'2002-12-04T12:30:00'
>>> parsed_date = datetime.datetime.strptime( mydate, "%Y-%m-%dT%H:%M:%S" )
>>> parsed_date
datetime.datetime(2002, 12, 4, 12, 30)

strptime has a flexible set of options as to parsing your date. See strftime() and strptime() Behavior for more information.

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • 3
    True, but there's no easy way to handle the Z suffix vs. a +/- offset. That requires additional coding. But I guess I'm going to have to do that. – vy32 May 13 '11 at 22:17
  • 23
    Downvote because this code only handles a very special, simple case of the ISO8601 standard and will crash on any other valid ISO8601 formatted date string. The question referes to the ISO8601 standard in general. – Daniel F Mar 14 '13 at 17:31
  • Agree with the above: as soon as you give `strptime()` an explicit format string, the input string _must_ be in that exact format. If any part is not in the input string (e.g. no offset specified if format string contains `%z`, it will raise an exception. – Cornel Masson Aug 23 '22 at 09:01
-11

If you install pandas, it comes with it. At least it happens to me.

C:\Users\dirak3d>pip install pandas
Collecting pandas
  Downloading https://files.pythonhosted.org/packages/a9/e8/ca7637c51767809cd7328dd01e246b8f2ec0fde566c9b7440b91d9a33460/pandas-0.23.3-cp37-cp37m-win32.whl (6.8MB)
    100% |████████████████████████████████| 6.8MB 137kB/s
Collecting pytz>=2011k (from pandas)
  Downloading https://files.pythonhosted.org/packages/30/4e/27c34b62430286c6d59177a0842ed90dc789ce5d1ed740887653b898779a/pytz-2018.5-py2.py3-none-any.whl (510kB)
    100% |████████████████████████████████| 512kB 216kB/s
Requirement already satisfied: numpy>=1.9.0 in c:\users\dirak3d\appdata\local\programs\python\python37-32\lib\site-packages (from pandas) (1.15.0)
Collecting python-dateutil>=2.5.0 (from pandas)
  Downloading https://files.pythonhosted.org/packages/cf/f5/af2b09c957ace60dcfac112b669c45c8c97e32f94aa8b56da4c6d1682825/python_dateutil-2.7.3-py2.py3-none-any.whl (211kB)
    100% |████████████████████████████████| 215kB 23kB/s
Collecting six>=1.5 (from python-dateutil>=2.5.0->pandas)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Installing collected packages: pytz, six, python-dateutil, pandas
anothernode
  • 5,100
  • 13
  • 43
  • 62
  • 3
    Why install `pandas` and more additional packages if you only want `python-dateutil`? Then you can just `pip install python-dateutil`. – wovano May 17 '19 at 23:21
  • There's two modules called dateutils and python-dateutils ```root@kali:~# pip freeze | grep dateutil dateutils==0.6.12 python-dateutil==2.8.2 ``` Hence, I think he meant to say the real one already comes from pandas maybe – Machinexa Aug 26 '21 at 19:00