Converting string representation of time to date time in app engine - python

In standard python, I can convert a string representation of time into datetime doing this:
date_string = u'Tue, 13 Sep 2011 02:38:59 GMT';
date_object = datetime.strptime(date_string, '%a, %d %b %Y %H:%M:%S %Z');
This works fine until I invoke the same over app engine where I get the error:
time data did not match format: data=2011-09-13 02:38:59 fmt=%a, %d %b %Y %H:%M:%S %Z
How would I convert this date string correctly so I can get a datetime representation?

Your error message indicates that you're not really passing Tue, 13 Sep 2011 02:38:59 GMT, but 2011-09-13 02:38:59. Are you sure you pass the correct parameters to strptime?
My python works just fine for the following:
datetime.strptime(u'Tue, 13 Sep 2011 02:38:59 GMT', "%a, %d %b %Y %H:%M:%S %Z")
# returns datetime.datetime(2011, 9, 13, 2, 38, 59)
This also works fine for me:
from dateutil imoprt parser as dparser
dparser.parse("Tue, 13 Sep 2011 02:38:59 GMT")
# returns datetime.datetime(2011, 9, 13, 2, 38, 59, tzinfo=tzutc())

Related

How to strip date time in python

From a website I'm getting a date in such format: Sun Jan 22 21:32:58 +0000 2012. I understand that I must get rid of +0000 to convert it to the date, but how exactly I can do it? I read the documentation but my code is not working:
from datetime import datetime
strDate = 'Mon Apr 29 14:30:53 2019'
objDate = datetime.strptime(strDate, '%a %b %H %M %S %Y')
I'm getting an error:
ValueError: time data 'Mon Apr 29 14:30:53 2019' does not match format '%d %m %H %M %S %Y'
And I don't really understand why. Or anyone knows how I can get a date from Sun Jan 22 21:32:58 +0000 2012?
If your object is datetime.datetime you can just simply do date()
from datetime import datetime
datetime1 = datetime.now()
date1 = datetime1.date()
One line solution:
strDate ='Sun Jan 22 21:32:58 +0000 2012'
objDate = datetime.strptime(strDate, '%a %b %d %H:%M:%S +%f %Y')
print(objDate)
#2019-04-29 14:30:53
Details:
You just forgot to use %d in order to capture the date number and the : for the time and you ALSO need to capture +0000.
Proof:
I'm afraid that the currently accepted answer, by seralouk, is incorrect. Using "+%f" turns the numbers into fractions of seconds. It's fine for 0000, but will mess things up if they happen to be anything else.
This is because the "+0000" part is a time zone offset, and the proper way to parse it is by using the "%z" directive, which will handle the "+" sign as well, so remove that from the format string:
>>> date_string = "Sun Jan 22 21:32:58 +0000 2012"
>>> datetime.strptime(date_string, "%a %b %d %H:%M:%S %z %Y")
datetime.datetime(2012, 1, 22, 21, 32, 58, tzinfo=datetime.timezone.utc)
You're missing colons : and the day format string %d. See the official documentation of strptime for a table that shows the different formatting values.
from datetime import datetime
strDate = 'Mon Apr 29 14:30:53 2019'
objDate = datetime.strptime(strDate, '%a %b %d %H:%M:%S %Y')
You can get rid of the '+0000' like this:
from datetime import datetime
strDate ='Sun Jan 22 21:32:58 +0000 2012'
objDate = datetime.strptime(strDate.replace(strDate.split(" ")[4] + " ", ""), '%a %b %d %H:%M:%S %Y')
print(objDate)
-> 2012-01-22 21:32:58
By the way,
1. The example of code that you post is not the one for the problem that you are asking about! (it does not include the +0000 in the string).
2. The error that you share (false format of the date, which has been answered already and described f.ex. here) is for another code, not the one you present above! (Error throws '%d %m %H %M %S %Y' instead of '%a %b %H %M %S %Y').
In your string u are missing the %d to catch the Day of month 01-31
'%a %b %d %H:%M:%S %Y'
If u want also to catch the +0000 u have to use the %z notation
Use this as a refrence to build your string correctly:
http://strftime.org/

python value error time data %Z

I am getting the following error:
ValueError: time data 'Tue, 17 Jul 2018 11:55:34 EDT' does not match format '%a, %d %b %Y %H:%M:%S %Z'
Code:
import datetime
dt = datetime.datetime.strptime('Tue, 17 Jul 2018 11:55:34 EDT', '%a, %d %b %Y %H:%M:%S %Z')
print(dt.timestamp())
Am I missing something here?
EDT is not identified as a valid timezone string. You may use something other intelligent parser and than automatically return the datetime object without you explicitly specifying the format. My suggestion is to go up with dateutil
>>> from dateutil import parser
>>> parser.parse('Tue, 17 Jul 2018 11:55:34 EDT')
datetime.datetime(2018, 7, 17, 11, 55, 34)

Value Error in Python when converting string to date using datetime.strptime

I have a string variable named dte that prints in the following format:
Tue Feb 13 23:49:10 +0000 2018
I'm trying to convert it to a date type so that I can work out how long ago it was in days, hours, minutes etc but keep getting the following error:
ValueError: time data 'Tue Feb 13 23:49:10 +0000 2018' does not match format '%a %b %d %I:%M:%S %z %Y'
I'm using this code:
new_date = datetime.datetime.strptime(dte, "%a %b %d %I:%M:%S %z %Y")
print(new_date)
I've tried removing the colons but get the same error. I'm sure it's something very simple I'm overlooking but can't figure out what it is.
Any help appreciated.
You need %H for the hour component not %I:
In[8]:
dte="Tue Feb 13 23:49:10 +0000 2018"
new_date = dt.datetime.strptime(dte, "%a %b %d %H:%M:%S %z %Y")
new_date
Out[8]: datetime.datetime(2018, 2, 13, 23, 49, 10, tzinfo=datetime.timezone.utc)
You have 24-hour hour component, not 12-hour which is what %I is for, see the docs

Error converting string to date: does not match format '%b %d %Y %I:%M%p'

I have a date in a string format: "Wed, 26 Apr 2017 12:39:28 GMT" and need to convert to datetime.
To do the conversion I am using datetime.strptime
Ex:
datetime.strptime ("Wed, 26 Apr 2017 12:39:28 GMT", '% b% d% Y% I:% M% p')
However, the error:
ValueError: time data 'Wed, 26 Apr 2017 12:39:57 GMT' does not match format '% b% d% Y% I:% M% p'
If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised. You should remove the redundant spaces and modify the directive which would be used to parse the given string.
you can try this:
>>> from datetime import datetime
>>>
>>> ds="Wed, 26 Apr 2017 12:39:28 GMT"
>>>
>>> datetime.strptime (ds, '%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2017, 4, 26, 12, 39, 28)
See more details from strftime() and strptime() Behavior
Like the error says, the string doesn't match the format. Try this:
datetime.datetime.strptime('Wed, 26 Apr 2017 12:39:28 GMT', '%a, %d %b %Y %H:%M:%S %Z')

ValueError parsing time string

I have written this code to convert a unusual time into EPOCH:
x = 'Mon Jul 25 19:04:30 GMT+01:00 2016'
print(datetime.strptime(x, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s'))
However, it returns the error ValueError: time data 'Mon Jul 25 19:04:30 GMT+01:00 2016' does not match format '%a %b %d %H:%M:%S %Z%z %Y'
The problem is something to do with the timezone. What have I done wrong?
Your timezone format has an extra : inside which causes the format mismatching error, you can remove the last : from the string firstly and then parse it:
import re
from datetime import datetime
x1 = re.sub(r":(?=[^:]+$)", "", x) # remove the last semi colon
datetime.strptime(x1, '%a %b %d %H:%M:%S %Z%z %Y').strftime('%s')
# '1469487870'
If you use dateutil instead of datetime.strptime it seems to work:
from dateutil import parser
parser.parse("Mon Jul 25 19:04:30 GMT+01:00 2016")
>> datetime.datetime(2016, 7, 25, 19, 4, 30, tzinfo=tzoffset(None, -3600))

Categories

Resources