I am trying to convert a string into date format in Python.
I am using following statement
datetime_obj = datetime.datetime.strptime("Sun Aug 19 16:24:31 PDT 2018", "%a %b %d %H:%M:%S %Z %Y")
However, I get an error -
ValueError: time data 'Sun Aug 19 16:24:31 PDT 2018' does not match format '%a %b %d %H:%M:%S %Z %Y'
If I remove the timezone from the date string and the format string, the code works perfect. Which leads me to believe that the issue is related to the timezone but I am not sure what actions should be taken.
I am in eastern timezone and the time zone in the string is in Pacific timezone.
Appreciate any help on this.
As mentioned in this answer you can use python-dateutil for this:
>>> from dateutil import parser
>>> datetime_obj = parser.parse("Sun Aug 19 16:24:31 PDT 2018")
datetime.datetime(2018, 8, 19, 16, 24, 31)
Standard datetime module behaves very strangely with parsing timezones, as I see reading this answer in question related to similar problem.
Related
In a script, I am converting the date string into DateTime format, so that I can modify the date, but this timezone part is showing an error.
from datetime import datetime
date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)'
temp_date = datetime.strptime(date_str, '%a, %d %b %Y %H:%M:%S %z (%Z)')
print(temp_date)
When I run this I am getting ValueEror.
ValueError: time data 'Wed, 01 Jun 2022 16:44:40 +0200 (CEST)' does not match format '%a, %d %b %Y %H:%M:%S %z (%Z)'
An extract from the datetime documentation:
strptime() only accepts certain values for %Z:
any value in time.tzname for your machine’s locale
the hard-coded values UTC and GMT
So someone living in Japan may have JST, UTC, and GMT as valid values,
but probably not EST. It will raise ValueError for invalid values
Try running the below to see if CEST is in your machine's locale:
import time
print(time.tzname)
Seeing the complexity of problem, I suggest using third party library like dateutil which can parse datetime with ease.
from dateutil.parser import parse
date_str = 'Wed, 1 Jun 2022 16:44:40 +0200 (CEST)'
temp_date = parse(date_str)
print(temp_date)
temp_date is of type datetime.datetime
https://dateutil.readthedocs.io/en/stable/
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/
I am trying to use strptime to format a time string that I have, but it gives the error of
"ValueError: unconverted data remains:"
without actually specifying what data it's missing
I'm not quite sure how to fix this problem. Here is what I've tried :
t = 'Tue, 26 Mar 2019 06:25:01 GMT'
def function(t):
timeString = t
date = datetime.datetime.strptime(timeString, '%a, %d %b %Y %H:%M:%s %Z')
I think maybe the issue is something to do with the floating point that's supposed to be there at the end. Any help would be appreciated. Also, any tips to convert this to another time zone would be great.
You can use dateutil library to parse datetime string.
Here is an example:
from dateutil import parser
datetime_string = 'Tue, 26 Mar 2019 06:25:01 GMT'
dt = parser.parse(datetime_string)
print(dt)
Output:
datetime.datetime(2019, 3, 26, 6, 25, 1, tzinfo=tzutc())
"ValueError: unconverted data remains:"
This indicates that datetime was unable to convert the data you supplied, probably due to a format error as it tried to parse an unexpected value.
Turns out that is the case, you have a typo. Change the s in the time format to capital S
date = datetime.datetime.strptime(timeString, '%a, %d %b %Y %H:%M:%S %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)
I'm trying to parse a given string of custom format date into epoch time with the following code:
time_tuple = time.strptime('Tue Dec 05 13:01:48 PST 2017', '%a %b %d %X %Z %Y')
time_epoch = time.mktime(time_tuple)
The given date format is as shown in the code. The problem I'm facing is when the code is compile I keep getting error saying:
ValueError: time data 'Tue Dec 05 13:01:48 PST 2017' does not match format '%a %b %d %X %Z %Y'
From what I can see, the format seems to be exactly as specified according to: https://docs.python.org/3/library/datetime.html
What seem's to be wrong?
Edit:
I have tried using datetime instead of time as well, but didn't work
time_tuple = datetime.strptime('Tue Dec 05 13:01:48 PST 2017', '%a %b %d %X %Z %Y')
Based on the comments below, it seems like the timezone field(%Z) is causing the problem. It seems like that field is based on the timezone the code is ran, and not the timezone the input string gives.
You are using a different time zone than those described in the docs, however, if you change the time zone, you can try this:
import time
time_tuple = time.strptime('Tue Dec 05 13:01:48 EST 2017', '%a %b %d %X %Z %Y')
time_epoch = time.mktime(time_tuple)