1
votes

I am using the date time lib to get the current date. After obtaining the current date I need to convert the obtained date to in to a string format. Any help is appriciated

from datetime import date
today=date.today()
print today
3
So is it date or time you want? - The Communist Duck
In the future, please highlight your code snippet in the editor and type Ctrl+K or click the {} button to format it. - wkl
What's wrong with what you have? print converts the value to a string as part of printing it. What more do you want? - S.Lott

3 Answers

7
votes

You can use today.strftime(format). format will be a string as described here http://docs.python.org/library/time.html#time.strftime . Example:

from datetime import date
today = date.today()
today.strftime("%x")
#>>> '01/31/11'
2
votes

datetime.strftime allows you to format a datetime however you want

1
votes
stringDate = str(today)

If that's what you want.