Format time string in Python 3.3

后端 未结 3 1919
逝去的感伤
逝去的感伤 2021-01-03 19:33

I am trying to get current local time as a string in the format: year-month-day hour:mins:seconds. Which I will use for logging. By my reading of the documentation I can do

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-03 20:15

    And for newer versions of Python (3.6+, https://www.python.org/dev/peps/pep-0498/ purely for completeness), you can use the newer string formatting, ie.

    import datetime
    
    today = datetime.date.today()
    
    f'{today:%Y-%m-%d}'
    > '2018-11-01'
    

提交回复
热议问题