Reputation: 2159
I have a string like 23 July 1914
and want to convert it to 23/07/1914
date format.
But my code gives error
.
from datetime import datetime
datetime_object = datetime.strptime('1 June 2005','%d %m %Y')
print datetime_object
Upvotes: 0
Views: 4213
Reputation: 2652
Here is what you should be doing:
from datetime import datetime
datetime_object = datetime.strptime('1 June 2005','%d %B %Y')
s = datetime_object.strftime("%d/%m/%y")
print(s)
Output:
>>> 01/06/05
You see your strptime
requires two parameters.
strptime(string[, format])
And the string will be converted to a datetime
object according to a format you specify.
There are various formats
%a - abbreviated weekday name %A - full weekday name %b - abbreviated month name %B - full month name %c - preferred date and time representation %C - century number (the year divided by 100, range 00 to 99) %d - day of the month (01 to 31) %D - same as %m/%d/%y %e - day of the month (1 to 31) %g - like %G, but without the century %G - 4-digit year corresponding to the ISO week number (see %V). %h - same as %b %H - hour, using a 24-hour clock (00 to 23)
The above are some examples. Take a look here for formats
Take a goood look at these two!
%b - abbreviated month name %B - full month name
It should be in a similar pattern to the string you provide. Confusing take a look at these examples.
>>> datetime.strptime('1 jul 2009','%d %b %Y')
datetime.datetime(2009, 7, 1, 0, 0)
>>> datetime.strptime('1 Jul 2009','%d %b %Y')
datetime.datetime(2009, 7, 1, 0, 0)
>>> datetime.strptime('jul 21 1996','%b %d %Y')
datetime.datetime(1996, 7, 21, 0, 0)
As you can see based on the format the string is turned into a datetime object. Now take a look!
>>> datetime.strptime('1 July 2009','%d %b %Y')
Traceback (most recent call last):
File "<pyshell#12>", line 1, in <module>
datetime.strptime('1 July 2009','%d %b %Y')
File "/usr/lib/python3.5/_strptime.py", line 510, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib/python3.5/_strptime.py", line 343, in _strptime
(data_string, format))
ValueError: time data '1 July 2009' does not match format '%d %b %Y'
Why error because jun
or Jun
(short form) stands for %b
. When you supply a June
it gets confused. Now what to do? Changed the format.
>>> datetime.strptime('1 July 2009','%d %B %Y')
datetime.datetime(2009, 7, 1, 0, 0)
Simple now converting the datetime object is simple enough.
>>> s = datetime.strptime('1 July 2009','%d %B %Y')
>>> s.strftime('%d/%m/%Y')
'01/07/2009
Again the %m
is the format for displaying it in months (numbers) read more about them.
Upvotes: 3
Reputation: 483
%d
means "Day of the month as a zero-padded decimal number."
%m
means "Month as a zero-padded decimal number."
Neither day or month are supplied what you tell it to expect. What you need it %B
for month (only if your locale is en_US), and %-d
for day.
Upvotes: 0
Reputation: 9863
This should work:
from datetime import datetime
print(datetime.strptime('1 June 2005', '%d %B %Y').strftime('%d/%m/%Y'))
print(datetime.strptime('23 July 1914', '%d %B %Y').strftime('%d/%m/%Y'))
For more info you can read about strftime-strptime-behavior
Upvotes: 0
Reputation: 152795
The placeholder for "Month as locale’s full name." would be %B
not %m
:
>>> from datetime import datetime
>>> datetime_object = datetime.strptime('1 June 2005','%d %B %Y')
>>> print(datetime_object)
2005-06-01 00:00:00
>>> print(datetime_object.strftime("%d/%m/%Y"))
01/06/2005
Upvotes: 0
Reputation: 1101
Your error is in the format you are using to strip your string. You use %m
as the format specifier for month, but this expects a 0 padded integer representing the month of the year (e.g. 06
for your example). What you want to use is %B
, which expects an month of the year written out fully (e.g. June
in your example).
For a full explanation of the datetime
format specifiers please consult the documentation, and if you have any other issues please check there first.
Upvotes: 7