O.rka O.rka - 1 year ago 79
Python Question

Convert datetime (e.g. 2016-01-01, 2016-01-11) to integer days (e.g. 0, 11) in Python

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?

2ps 2ps
Answer Source
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