So I'm using pyodbc to take a Date Time field from MS Access add to a Python list. When I do this, it pyodbc instantly converts the data to this format datetime.datetime(2012, 1, 1,0,0)
. I'm only interested in obtaining the year 2012
in this case. How can I parse the year out of my List when it uses this format? Maybe pyodbc has some syntax I could use before it evens gets into the List?
import datetime; today = str(datetime. date. today()); curr_year = int(today[:4]); curr_month = int(today[5:7]); This will get you the current month and year in integer format.
To get the current year in Python, first we need to import the date class from the datetime module and call a today(). year on it. The year property returns the current year in four-digit(2021) string format according to the user's local time.
You can grab the year from each of the datetime objects and form a new list.
years = [x.year for x in your_list]
>>> dt = datetime.datetime(2012, 1, 1,0,0)
>>> dt.year
2012
Just for the record, datetime.datetime
is not a "list of values", it's a class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With