Datetime current year and month in Python

后端 未结 7 1212
情话喂你
情话喂你 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:27

    You can write the accepted answer as a one-liner using date.replace:

    datem = datetime.today().replace(day=1)
    

提交回复
热议问题