How to extract the year from a Python datetime object?

后端 未结 4 1241
野的像风
野的像风 2021-01-30 12:22

I would like to extract the year from the current date using Python.

In C#, this looks like:

 DateTime a = DateTime.Now() 
 a.Year

What

4条回答
  •  星月不相逢
    2021-01-30 12:45

    import datetime
    a = datetime.datetime.today().year
    

    or even (as Lennart suggested)

    a = datetime.datetime.now().year
    

    or even

    a = datetime.date.today().year
    

提交回复
热议问题