Convert date and time to Python list only in year

So, I use pyodbc to take the Date Time field from MS Access, add Python to the list. When I do this, it pyodbc instantly converts the data to this datetime.datetime(2012, 1, 1,0,0) format. In this case, I am only interested in the year 2012 . How can I analyze the year from my list when it uses this format? Maybe pyodbc has some syntax that I could use before it appears in the list?

+4
source share
2 answers
 >>> dt = datetime.datetime(2012, 1, 1,0,0) >>> dt.year 2012 

For writing purposes only, datetime.datetime not a "list of values"; it is a class.

+8
source

You can capture a year from each datetime object and form a new list.

 years = [x.year for x in your_list] 
+1
source

Source: https://habr.com/ru/post/1412842/


All Articles