1

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
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
joel james
  • 11
  • 1
  • 2

4 Answers4

7

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'
Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
2

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

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
1
stringDate = str(today)

If that's what you want.

PrettyPrincessKitty FS
  • 6,117
  • 5
  • 36
  • 51
1

In Python3, you can use f-string formatting:

from datetime import date 
today=date.today()
today_str = f"{today:%m/%d/%y}"
today_str
#>>>'06/24/22'
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33