如何打印今天的下三个日期?

时间:2019-04-18 16:26:49

标签: python date datetime

我有一个代码,可以告诉您所输入位置的天气。 我想选择打印接下来3天的天气,我需要将3个日期(带循环)发送到我的函数中,每次不同的日期,如何发送当前日期之后3天的日期? / p>

#This is my function
def weather(city, date): 

#This is the part where I send it from the main to the function:
city = 'Paris'
while(i < 4):
    i += 1
    weather(city.lower(), dd/mm/yyyy)# Here instead of "dd/mm/yyyy" I need to send every time the next date from today.

2 个答案:

答案 0 :(得分:0)

使用datetime.timedelta(days=1)按如下所示将一天增加1,您可以将此日期传递给函数

import datetime
curr_date = datetime.datetime.now()

for i in range(4):
    curr_date += datetime.timedelta(days=1)
    print(curr_date)
#2019-04-19 22:01:29.503352
#2019-04-20 22:01:29.503352
#2019-04-21 22:01:29.503352
#2019-04-22 22:01:29.503352

答案 1 :(得分:0)

生成日期范围的最简单方法是使用pandas.date_range作为

import pandas as pd

dates = pd.date_range('2019-04-10', periods=3, freq='D')

for day in dates:
    weather(city, day)

或者您可能会坚持要求隔天循环,您可以使用datetime.timedelta

from datetime import date, timedelta

one_day_delta = timedelta(1)
day = datetime.date(2019, 4, 19)
for i in range(3)
    day += one_day_delta
    weather(city, day)