Search code examples
pythondatetime

Datetime current year and month in Python


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")

Is there maybe another way?


Solution

  • Use:

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

    I assume you want the first of the month.