2

I have a large list of dates that are datetime objects like for example

[datetime.datetime(2016,8,14),datetime.datetime(2016,8,13),datetime.datetime(2016,8,12),....etc.]

Instead of datetime objects of the date what I want instead is a list of numerical integer values since the date 1/1/1900. I have defined 1/1/1900 as the base date and in the for loop below, I have calculated the days between the date in the list since that base date:

baseDate = datetime(1900,1,1)
numericalDates = []
for i in enumerate(dates):
    a=i[1]-baseDate
    numericalDates.append(a)

print(numericalDates)

However when I print this out, I get datetime.timedelta objects instead

[datetime.timedelta(42592), datetime.timedelta(42591), datetime.timedelta(42590),...etc.]

Any ideas on how I can convert it the proper way?

bwrr
  • 611
  • 1
  • 7
  • 16
  • You need to use 30/12/1989 for the exact equivalent of Excel's by the way: http://stackoverflow.com/q/9574793/2285236 (see the comments in the accepted answer). – ayhan Aug 14 '16 at 09:08

1 Answers1

3

timedelta objects have days attribute, so you can simply append that as an int:

numericalDates.append(a.days)

will result with numericalDates being [42594, 42593, 42592].

Note that you can also simplify your code a bit by using list comprehension:

numericalDates = [(d - baseDate).days for d in dates]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154