5
from datetime import datetime
current_time = datetime.now()

print "%s-%s-%s" % (current_time.month, current_time.day current_time.year,)
#Will print the current date in a mm/dd/yyyy format.

input()

The code above is meant to print out the current date in a mm/dd/yyyy format in a command prompt. So for example if this actually worked it would open up a command prompt window that printed out; for example the current date as im writing this like this: 8-5-2017

I keep getting this error when trying to run the module that the closing " in "%s-%s-%s" that it's invalid syntax. Is python 3 using something different from this or did I make a mistake?

cs95
  • 379,657
  • 97
  • 704
  • 746
Volerm
  • 67
  • 1
  • 1
  • 2

1 Answers1

14

In python3, print statements require braces.

Furthermore, if you're looking to print the data, just use datetime.strftime

In [340]: print(current_time.strftime('%m/%d/%Y'))
Out[340]: '08/05/2017'
Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
cs95
  • 379,657
  • 97
  • 704
  • 746
  • Thanks, I forgot that print required brackets. Too much time on code academy and not using the editor slipped me up on that. – Volerm Aug 05 '17 at 08:10
  • If you don't want the month or day to have leading zeros then on Linux/Unix use `%-m/%-d/%Y`. For Windows use `%#m/%#d/%Y` . – Shane S Jan 12 '23 at 01:40