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')
Related
I get an error when I try to convert a datetime string to a datetime object:
df['R_DATE'] = pd.to_datetime(df['R_DATE'], format='%a %b %d %H:%M:%S %Z %Y')
Error is:
...
File "pandas\_libs\tslibs\strptime.pyx", line 141, in pandas._libs.tslibs.strptime.array_strptime
ValueError: time data 'Mon Oct 18 00:00:00 EDT 2021'
does not match format '%a %b %d %H:%M:%S %Z %Y' (match)
From what I can tell format appears to match the datetime string value. I'm not sure if the timezone value (EDT) is causing issues.
nvm. found the answer I was looking for.
import dateutil
tzdict = {'EST': dateutil.tz.gettz('America/New_York'),
'EDT': dateutil.tz.gettz('America/New_York')}
df['R_DATE'] = df['R_DATE'].apply(dateutil.parser.parse, tzinfos=tzdict)
from datetime import datetime
y='Monday, December 9, 2019'
I want to convert the above string to DD/MM/YYYY I tried
c=datetime.strptime(y,'%A, %B %-d,%Y')
so I can then easily convert it but it is giving me ValueError: '-' is a bad directive in format '%A, %B %-d,%Y I checked this question
'-' is a bad directive in format '%Y-%-m-%-d' - python/django but still gives error, is there a way to do this without using re ?
The correct format is '%A, %B %d, %Y' (noticed the removed -), and to change it to DD/MM/YYYY, the format is %d-%m-%Y'
from datetime import datetime
y='Monday, December 9, 2019'
#Fixed format
c=datetime.strptime(y,'%A, %B %d, %Y')
#Changed to represent DD/MM/YYYY
print(c.strftime('%d-%m-%Y'))
The output will be
09-12-2019
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.
How to convert GMT time to string time? it's quite strange, when I print the time in back-end, the time is in string time format, but when I transfer it to the front-end via JSON file, the time has changed to GMT format. Why? Could someone help me?
Input: Wed, 25 Jul 2018 19:19:42 GMT
Output: 2018-07-25 19:19:42
When you convert the original time to JSON Format, it will be changed to GMT Time format. To solve this problem, you can change the time to string before converting it to JSON.
You could do something like -
from datetime import datetime
ip = 'Wed, 25 Jul 2018 19:19:42 GMT'
op = datetime.strftime(datetime.strptime(ip,'%a, %d %b %Y %H:%M:%S %Z'), '%Y-%m-%d %H:%M:%S')
# op 2018-07-25 19:19:42
Refer to this beautiful table here to get the formats right and then look into strftime and strptime
You can find resources here on strptime
from datetime import datetime
t = 'Wed, 25 Jul 2018 19:19:42 GMT'
datetime.strptime(t,'%a, %d %b %Y %H:%M:%S %Z')
datetime.datetime(2018, 7, 25, 19, 19, 42)
I work with api in python3 in this api return date like this
'Jun 29, 2018 12:44:14 AM'
but i need just hours, minute ad second like this
12:44:14
are there a fonction that can format this
It looks like the output is a string. So, you can use string slicing:
x = 'Jun 29, 2018 12:44:18 AM'
time = x[-11:-3]
It's best to use negative indexing here because the day may be single-digit or double-digit, so a solution like time = x[13:21] won't work every time.
If you're inclined, you may wish to use strptime() and strftime() to take your string, convert it into a datetime object, and then convert that into a string in HH:MM:SS format. (You may wish to consult the datetime module documentation for this approach).
Use the datetime module. Use .strptime() to convert string to datetime object and then .strftime() to convert to your required string output. Your sample datetime string is represented as '%b %d, %Y %H:%M:%S %p'
Ex:
import datetime
s = 'Jun 29, 2018 12:44:14 AM'
print( datetime.datetime.strptime(s, '%b %d, %Y %H:%M:%S %p').strftime("%H:%M:%S") )
Output:
12:44:14