Question

I have some doubts in the dateformat Tue Feb 25 2014 00:00:00 GMT+0530 (IST).

  • does Tue Feb 25 2014 00:00:00 means GMT or IST
  • Is it possible to convert this into python datetime.
  • and also is it possible convert it into format DD-MM-YY,HH:MM:SS in GMT.

Here is what i tried to convert into python datetime::

but i got error,when i tried with %z:

>>> time_format="%a %b %d %Y %H:%M:%S GMT%z (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT+0530 (IST)"
>>> mydate=datetime.strptime(v,time_format)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/_strptime.py", line 317, in _strptime
    (bad_directive, format))
ValueError: 'z' is a bad directive in format '%a %b %d %Y %H:%M:%S GMT%z (%Z)'

But this works:

>>> time_format="%a %b %d %Y %H:%M:%S GMT (%Z)"
>>> v="Tue Feb 25 2014 00:00:00 GMT (IST)"
>>> datetime.strptime(v,time_format)    
datetime.datetime(2014, 2, 25, 0, 0)

but still didn't understand anything about TIMEZONE.

Was it helpful?

Solution

In the system terminal

easy_install python-dateutil

In the python shell

from dateutil import parser as date_parser
print date_parser.parse("Tue Feb 25 2014 00:00:00 GMT+0530 (IST)")

Dateutil's parse can typically just parse anything you throw at it into a Python datetime.

OTHER TIPS

I think the best way would be to try something like this, especially if you want to avoid using external libraries for example in Python 3.6.9 :

datetime.strptime(v,"%a %b %d %Y %H:%M:%S %Z%z (IST)")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top