I have the following code (based on http://strftime.org/):
try:
datetime.datetime.strptime("Apr 14, 2016 9", '%b %d, %Y %-I')
print "matched date format"
except ValueError:
print "did NOT match date format"
The above prints:
$ python parse_log.py
did NOT match date format
However bash recognizes this date format:
$ date '+%b %d, %Y %-I'
Apr 14, 2016 1
What am I missing?
It seems that the %-I is the problem, since Python matches date without the %-I section:
try:
datetime.datetime.strptime("Apr 14, 2016 ", '%b %d, %Y ')
print "matched date format"
except ValueError:
print "did NOT match date format"
output:
$ python parse_log.py
matched date format
I'm on python 2.6.6.
The actual pattern I need to match uses 12 hour clock and is:
datetime.datetime.strptime("Apr 14, 2016 9:59:54", '%b %d, %Y %-I:%M:%S')
You need to remove the - for strptime:
'%b %d, %Y %I:%M:%S'
In [17]: print datetime.datetime.strptime("Apr 14, 2016 9:59:54", '%b %d, %Y %I:%M:%S')
2016-04-14 09:59:54
The -I is used only for strftime:
In [15]: print datetime.datetime.strptime("Apr 14, 2016 9:59:54", '%b %d, %Y %I:%M:%S').strftime('%b %d, %Y %-I:%M:%S')
Apr 14, 2016 9:59:54
Related
I have a web app , using Django as backend.
I used datetime.strptime function in python3 to convert the date to the format need to input to Mysql database.
But I got the error: ValueError: time data 'July 31, 2021' does not match format '%m %d, %Y'
end_date = request.GET.getlist('end_date')[0] # end_date = 'July 31, 2021' in the test case
end_date_converted = datetime.strptime(end_date, "%m %d, %Y").strftime("%Y-%m-%d")
How could I convert 'July 31, 2021' to YYYY-MM-DD format so I could save it to MYSQL date column?
According to docs %m is "Month as a zero-padded decimal number", not the month name. You should be using
%B %d, %Y
as the format specifier. For example:
>>> datetime.strptime('July 31, 2021', '%B %d, %Y').strftime('%Y-%m-%d')
'2021-07-31'
replace %m with %B which will decode the Month full name
I have a dataset with abbreviated month names, and I have tried to follow some other solutions posted here, such as :
r.Date = pd.to_datetime(r.Date, format='%MMM %d, %Y')
but unfortunately it is giving me a ValueError: time data 'Nov 13, 2020' does not match format '%d %B, %Y' (match). The months dates are all abbreviated.
Change to
pd.to_datetime('Nov 13, 2020',format='%b %d, %Y')
Out[23]: Timestamp('2020-11-13 00:00:00')
Not sure why I'm getting this error when trying to parse a string into datetime.
This is the code I have:
date = datetime.strptime("13 Aug 05", '%d %m %y')
and it is raising this error:
ValueError: time data '13 Aug 05' does not match format '%d %m %y'
date = datetime.strptime("13 Aug 05", '%d %b %y')
You need to use %b, not %m, because your string uses the month's 3-letter abbreviated name and not the zero-padded decimal number.
I am a beginner in python automation with selenium bindings.
I have a Unicode string in the format like Monday , December 12, 2016 , 00:00:23 PM.
I am using the below command
datetime.strptime(date, "%A, %B %d, %Y, %I:%M:%S %p")
But I am getting error that time data doesn't match the format.
Please help me on this issue.
I tried like this,
print datetime.datetime.strptime(u'Monday , DECEMBER 12, 2016 , 00:00:23 PM' , '%A , %B %d, %Y , %H:%M:%S %p')
and the output:
2016-12-12 00:00:23
Try
import time
time.strftime("%A, %B %d, %Y, %I:%M:%S %p", time.gmtime())
and make sure the space or other symbols are not missing.
I'm trying to convert a string given in "DD MM YYYY" format into a datetime object. Here's the code for the same:
from datetime import date, timedelta
s = "23 July 2001"
d = datetime.datetime.strptime(s, "%d %m %Y")
However, I get the following error:
ValueError: time data '23 July 2001' does not match format '%d %m %Y'
What's wrong ? Isn't the format specified in the string the same as that specified by "%d %m %Y" ?
%m means "Month as a zero-padded decimal number."
Your month is July so you should use %B, which is "Month as locale’s full name."
Reference: https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior
Like behzad.nouri said, Use d = datetime.datetime.strptime(s, "%d %B %Y").
Or make s = '23 07 2001' and d = datetime.datetime.strptime(s, "%d %m %Y")