Reputation: 21
for example how would you get "2016" from
Fri, Feb, 19 ?
I have a database from somewhere that has entries from the last 4 years but only lists dates as day of the week and day of the month, and I need to get the year from each. There should not be any repetitions because there is only four years of data, and date & day of the week alignments do not repeat in that timeframe.
Upvotes: 2
Views: 91
Reputation: 2144
This is just slightly different from @zondo's answer who beats me to it, but I'll put this up anyway because I prefer not to have the .year
at the end of a long expression:
year = next(y for y in range(2013, 2017) if datetime.date(y, 2, 19).weekday() == 4)
Upvotes: 3
Reputation: 20336
How about this:
import datetime
year = next(date for year in (2013, 2014, 2015, 2016)
for date in (datetime.date(year, 2, 19),)
if date.weekday() == 4).year
Upvotes: 3