I have a script that has been written that I need to change the date on, when the script runs it outputs this message
output_messaging("*** DATA REFRESH FOR STREETWORKS DATA EXECUTED ON " +
str(datetime.date.today().day) + "/" + str(datetime.date.today().month) + "/" +
str(datetime.date.today().year) + " AT " + str(datetime.datetime.now().hour) + ':' +
str(datetime.datetime.now().minute) + ':' + str(datetime.datetime.now().second) + " ***\n\n")
Which gives
*** DATA REFRESH FOR STREETWORKS DATA EXECUTED ON 20/2/2012 AT 18:3 ***
I need the date converted to
20/Feb/2012 , how do i do this?
Use
strftimefor this — it’s way easier than doing a bunch of custom concatenation. There’s a table of formatting conventions here.As eumiro‘s answer indicates, you can also invert the above call with a slightly modified format string like so:
This works because
formatsimply calls the (shockingly under-documented)__format__method of thedatetime.datetimeobject. For example:This works for other types too:
For more on
__format__, see PEP 3101