Datetime current year and month in Python

后端 未结 7 994
情话喂你
情话喂你 2020-12-07 21:33

I must have the current year and month in datetime.

I use this:

datem = datetime.today().strftime(\"%Y-%m\")
datem = datetime.strptime(datem, \"%Y-%m         


        
相关标签:
7条回答
  • 2020-12-07 22:28

    Use:

    from datetime import datetime
    today = datetime.today()
    datem = datetime(today.year, today.month, 1)
    

    I assume you want the first of the month.

    0 讨论(0)
提交回复
热议问题