Trying to parse a date like, "Thu, 22 Oct 2020 17:33:00 UT" with datetime.strptime(). I've tried:
datetime.strptime('Thu, 22 Oct 2020 17:33:00 UT', '%a, %d %b %Y %H:%M:%S %Z')
However, it appears 'UT' is not a recognized timezone. Is this possible without any string manipulation? Any help is much appreciated.
you could use dateutil's parser and supply a tzinfos mapping dict:
import dateutil
timezone_infos = {"UT": dateutil.tz.UTC}
dt = dateutil.parser.parse('Thu, 22 Oct 2020 17:33:00 UT', tzinfos=timezone_infos)
print(dt)
>>> 2020-10-22 17:33:00+00:00
Related
I am trying to convert this format or any timezone format to YYYY-MM-DD HH:MM:SS.
format needed to be converted is - Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time).
can anyone help ?
from datetime import datetime
date_string = 'Tue Sep 07 2021 08:34:00 GMT+0530 (India Standard Time)'.replace('(India Standard Time)', '').rstrip()
datetime_object = datetime.strptime(date_string, '%a %b %d %Y %H:%M:%S %Z%z').strftime("%Y-%m-%d %H:%M:%S")
print(datetime_object)
Gives output
2021-09-07 08:34:00
Hour is set to 24 hour clock
If you want 12 hour clock replace %H with %I in the above code
I got a date in format like this: Wed, 15 Jul 2020 00:00:00 GMT i need to make it to %Y-%m-%d something like 2020-07-15
So, in test3 variable data is Wed, 15 Jul 2020 00:00:00 GMT
I try something like this
date_time = test3.strftime("%Y-%m-%d", test3)
writing `AttributeError: 'str' object has no attribute 'strftime'
I have imported
from datetime import datetime
So any ideas on how to convert it?
from datetime import datetime
test3 = "Wed, 15 Jul 2020 00:00:00 GMT"
date_time = datetime.strptime(test3, "%a, %d %b %Y %H:%M:%S %Z").strftime("%Y-%m-%d")
print(date_time)
use the code:
from datetime import datetime
time_str='Wed, 15 Jul 2020 00:00:00'
dt=datetime.strptime(time_str,'%a, %d %b %Y %H:%M:%S')
print(dt.strftime('%Y-%m-%d'))
I have a function in my backend and I receive the date from the front end in the formart Wed Dec 14 2016 14:39:16 GMT+0300 (AST)
date = request.body['date']
d = datetime.strptime(date, '%a %b %d %Y %X %Z%z')
I know strptime converts to the datetime object but I keep getting the following
'z' is a bad directive in format '%a %b %d %Y %X %Z%z'
What should my string format be?
You can do like this:
from dateutil import parser
parser.parse("Wed Dec 14 2016 14:39:16 GMT+0300 (AST)")
How to convert "Tue Aug 25 10:00:00 2015" this time stamp to "2015-08-25 10:00" in python.
from datetime import datetime
date_object = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')
With the correct format string, you can use datetime.strptime to parse the string and format it again:
import datetime
date = datetime.datetime.strptime('Tue Aug 25 10:00:00 2015', '%a %b %d %H:%M:%S %Y')
print date.strftime('%Y-%m-%d %H:%M')
use parser using pip install python-dateutil
>>>from dateutil import parser
>>>str(parser.parse("Tue Aug 25 10:00:00 2015"))
'2015-08-25 10:00:00'
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.
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.
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)")