0

I have the data with an array containing dates (YYYY-MM-DD) starting from 2005-12-01 till 2012-30-12. The dates are irregular and some of the dates are missing in between. I want to take the reference date as 2005-11-30 and calculate the integer number of all the dates in the array.

How can I convert my date array into an integer number from the reference date in Python?

Madhavan
  • 49
  • 1
  • 2
  • 11
  • 1
    examples please... example of input and output would do a world of good...would stop us from guessing – Vivek Kalyanarangan Mar 02 '17 at 07:56
  • have you tried http://stackoverflow.com/questions/151199/how-do-i-calculate-number-of-days-betwen-two-dates-using-python – Clock Slave Mar 02 '17 at 07:56
  • Possible duplicate of [How do I calculate number of days betwen two dates using Python?](http://stackoverflow.com/questions/151199/how-do-i-calculate-number-of-days-betwen-two-dates-using-python) – Clock Slave Mar 02 '17 at 07:57

1 Answers1

0

If I understood your question correctly you have a list of dates which you want to find and write the difference of each between a fixed date. You can use list comprehension;

from datetime import date
start_date = date(2005, 11, 30)

# assuming your list is named my_date_list
differences = [d - start_date for d in my_date_list]

If your list members are not date type, but formatted strings. You can convert them to dates on the run.

from datetime import datetime
date_format = "%Y-%m-%d"
differences = [datetime.strptime(d, date_format) - start_date for d in my_date_list]
umutto
  • 7,460
  • 4
  • 43
  • 53