0
df = pd.read_csv(dataname, sep=',')

# get rid of the format issue
df.rename(columns={df.columns[0]: 'Date' }, inplace=True)

# select desired columns
df = df[['Date', 'Close']]

# define your format in the Date column
date_format = '%Y-%m-%d'

#initialize an empty string
numdays=['ha']    #need to add a column title

# select the first date
date1 = datetime.strptime(df['Date'][len(df['Date'])-1], date_format)

for i in range(0,len(df['Date'])):
# select the second date
    date2 = datetime.strptime(df['Date'][i], date_format)

    # calculate the difference between the dates
    diffDates = date2 - date1

    numdays.append(diffDates.days)   #numb of days between two given dates 


# calculate the difference between the dates
diffDates = date2 - date1

Above is what i have to determine the number of days between two given dates.

Now, I'd like to do the opposite.

Suppose you know how many days have passed since 1/03/2008 for example,

Is there a simple way in Python to find out what month/day/year it is now? given the number of days between 1/03/2008 and now?

stratofortress
  • 395
  • 1
  • 8
  • 18

4 Answers4

0
from datetime import date, timedelta

YOUR_DATE = date.today() - timedelta(days=NUMBER_OF_DAYS)
jood
  • 1,773
  • 2
  • 15
  • 32
0

Use the date class from the datetimemodule. To get the current date, use .today()method.

An easy way to count days is to subtract the ordinal values. Another way is to use timedelta class.

from datetime import date
d1 = date( 2008, 3, 1 )    # Y,M,D 
d2 = date.today()
diff_days_v1 = d2.toordinal() - d1.toordinal()
diff_days_v2 = ( d2 - d1 ).days
Sci Prog
  • 2,503
  • 1
  • 7
  • 17
0
from datetime import date, timedelta

YOUR_DATE = date(2008, 03, 01) + timedelta(days=NUMBER_OF_DAYS)

Also check Link

Community
  • 1
  • 1
Prashant Puri
  • 2,104
  • 1
  • 11
  • 20
-1

I am Considering you want to find now date just by passing total no of days from given date to now date

Try this,

import datetime
from datetime import timedelta
old_date = datetime.datetime.strptime('2008-03-01', '%Y-%m-%d')
t = datetime.datetime.now() - old_date
now_date = old_date + timedelta(days=t.days)
now_date = datetime.datetime.strftime(now_date, '%m/%d/%Y') # As you want format like month/date/year
print now_date
NiviD
  • 246
  • 2
  • 14