TIMEX TIMEX - 2 months ago 12
Python Question

How do I get a string format of the current date time, in python?

For example, on July 5, 2010, I would like to calculate the string

July 5, 2010


How should this be done?

Answer

You can use the datetime module for working with dates and times in Python. The strftime method allows you to produce string representation of dates and times with a format you specify.

>>> import datetime
>>> datetime.date.today().strftime("%B %d, %Y")
'July 23, 2010'
>>> datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y")
'10:36AM on July 23, 2010'
Comments