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

Question

Asked By – TIMEX

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

 July 5, 2010

How should this be done?

Now we will see solution for issue: How do I get a string format of the current date time, in python?


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'

This question is answered By – David Webb

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0