How to extract the year from a Python datetime object?

后端 未结 4 1240
野的像风
野的像风 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:55

    If you want the year from a (unknown) datetime-object:

    tijd = datetime.datetime(9999, 12, 31, 23, 59, 59)
    
    >>> tijd.timetuple()
    time.struct_time(tm_year=9999, tm_mon=12, tm_mday=31, tm_hour=23, tm_min=59, tm_sec=59, tm_wday=4, tm_yday=365, tm_isdst=-1)
    >>> tijd.timetuple().tm_year
    9999
    

提交回复
热议问题