0

I am trying to find how many of "First Day of the year" occurs between a range of two dates.

Example.

service_start = datetime.date(2012, 5, 6)
service_end = datetime.date(2015, 7, 24)

What I am trying to see is how many times January 1st occurs between the above range. In the above example the answer will be 3

satoru
  • 31,822
  • 31
  • 91
  • 141
Othman
  • 2,942
  • 3
  • 22
  • 31
  • possible duplicate of [Counting day-of-week-hour pairs between two dates](http://stackoverflow.com/questions/31574971/counting-day-of-week-hour-pairs-between-two-dates) – TigerhawkT3 Aug 08 '15 at 00:03
  • Google: "January 1st euler problem python", why wasting our time with things that have been solved a thousand times? – Ayy Aug 08 '15 at 00:56

2 Answers2

3

I don't know if I understand the question properly but... I think you could do it just by getting the difference between the years, with just the special case where the first date starts on January 1st as well, where you'd add one.

service_start = datetime.date(2012, 5, 6)
service_end = datetime.date(2015, 7, 24)
first_days = (service_end.year - service_start.year) + (1 if service_start.month == 1 and service_start.day == 1 else 0)
José Tomás Tocino
  • 9,873
  • 5
  • 44
  • 78
0

Given that to transition between any two years requires incrementing the "year" by one, and that this implies passing the first day of the year (or at least reaching it), therefore, subtraction of the "year" value is the simplest way to determine that value.

years = service_end.year - service_start.year

NB: You would perhaps want to consider what edge cases such as 2012,01,01 to 2015,01,01 should be - i.e. if the range ends on the first day of the year, does that count as between?

Mike Curry
  • 1,609
  • 1
  • 9
  • 12