Display the current date in a mm/dd/yyyy format in Python

18,834

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'
Share:
18,834
Admin
Author by

Admin

Updated on July 30, 2022

Comments

  • Admin
    Admin almost 2 years
    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?

  • Admin
    Admin almost 7 years
    Thanks, I forgot that print required brackets. Too much time on code academy and not using the editor slipped me up on that.