1

Is it possible to convert a timestamp object in the format "2016-01-01" (year month day) into number of days from new years?

I'm getting my timestamp object by str(pandas.to_datetime('today')).split(" ")[0]

I would put code that I used to get it started but I don't even know where to begin except by making a dictionary for how many days are in each month and then calculating it the long way. Is there something built in that I can use for this?

O.rka
  • 29,847
  • 68
  • 194
  • 309
  • The `datetime.strptime` method and the `timedelta` method would be useful to you – OneCricketeer Apr 24 '16 at 00:19
  • 2
    please see [this](http://stackoverflow.com/questions/466345/converting-string-into-datetime) and [this](http://stackoverflow.com/questions/151199/how-do-i-calculate-number-of-days-betwen-two-dates-using-python) – OneCricketeer Apr 24 '16 at 00:23
  • @cricket_007 - The second link is useful here, but the OP is starting with a `datetime` and then converting it with `str()` for some reason, so instead of converting that back to `datetime` they can just dispense with `str()` in the first place. – TigerhawkT3 Apr 24 '16 at 00:28
  • @TigerhawkT3 It wasn't clear what the types were, so I provided both, just in case – OneCricketeer Apr 24 '16 at 00:31

1 Answers1

1
from datetime import datetime, timedelta, date
import dateutil
st_date = '2016-11-01'
dt = dateutil.parser.parse(st_date).date()
tm_diff = dt - date(2016, 1, 1);
print tm_diff.days
2ps
  • 15,099
  • 2
  • 27
  • 47