如何确定日期,知道自给定日期以来已经过了多少天

时间:2016-02-24 04:57:51

标签: python python-2.7

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

以上是我必须确定两个给定日期之间的天数。

现在,我想做相反的事情。

假设您知道自2008年1月1日以来经过了多少天,例如

Python中是否有一种简单的方法可以找出现在的月/日/年?考虑到2008年3月1日至今之间的天数?

4 个答案:

答案 0 :(得分:0)

from datetime import date, timedelta

YOUR_DATE = date.today() - timedelta(days=NUMBER_OF_DAYS)

答案 1 :(得分:0)

使用<!DOCTYPE html> <HTML> <TITLE>Img Tester</TITLE> <BODY> <h1>Image:</h1> <img src ="link to src"/> <!--only display this if the variable c == true in the script--> </BODY> <SCRIPT> var c = confirm("Display Image?"); if(c== true) { //do something here that displays image? } </SCRIPT> </HTML> 模块中的date类。要获取当前日期,请使用datetime方法。

计算天数的简单方法是减去序数值。另一种方法是使用.today()类。

timedelta

答案 2 :(得分:0)

from datetime import date, timedelta

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

Also check Link

答案 3 :(得分:-1)

我认为你想通过将从给定日期到现在日期的总天数通过来找到现在的日期

试试这个,

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