I have a date string with the format 'Mon Feb 15 2010'. I want to change the format to '15/02/2010'. How can I do this?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
use datetime library http://docs.python.org/library/datetime.html look up 9.1.7. especiall strptime() strftime() Behavior¶ examples http://pleac.sourceforge.net/pleac_python/datesandtimes.html
Just for the sake of completion: when parsing a date using
strptime()
and the date contains the name of a day, month, etc, be aware that you have to account for the locale.It's mentioned as a footnote in the docs as well.
As an example:
convert string to datetime object
As this question comes often, here is the simple explanation.
datetime
ortime
module has two important functions.In both cases, we need a formating string. It is the representation that tells how the date or time is formatted in your string.
Now lets assume we have a date object.
If we want to create a string from this date in the format
'Mon Feb 15 2010'
Lets assume we want to convert this
s
again to adatetime
object.Refer This document all formatting directives regarding datetime.
@codeling and @user1767754 : The following two lines will work. I saw no one posted the complete solution for the example problem that was asked. Hopefully this is enough explanation.
Output: