This question already has answers here:
Generating dates to the days of a week in Python?
(3 answers)
Python - Add days to an existing date
(1 answer)
Adding days to a date in Python
(16 answers)
Closed 3 years ago.
I have a code that can tell the weather in entered location.
I want to make an option to print the weather for the next 3 days ,I need to send to my function 3 dates (with a loop), every time different date, how can I send dates of next 3 days from current day?
#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.
Use datetime.timedelta(days=1) to increment your day by 1 as follows, you can pass this date to your function
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
The simplest way to generate a date range is to use pandas.date_range as
import pandas as pd
dates = pd.date_range('2019-04-10', periods=3, freq='D')
for day in dates:
weather(city, day)
Or you may insist on a loop for next days, you can use 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)
Related
I am trying to find the first and last date of the previous five months from now using python.
today = datetime.today()
first = today.replace(day=1)
lastMonth = first - timedelta(days=153)
Now how do i find the first and last date of each of the previous five months in python?
Can anyone help me with this please?
The below is based off an two answers already on SO. It adds in the ability to respond with the first and last day over a range of dates.
Get year month for last X months
How to get the first and last day of the month
import calendar
import datetime
from dateutil.relativedelta import relativedelta
def get_last_months(start_date, months):
for i in range(months):
_, num_days = calendar.monthrange(start_date.year,start_date.month)
first_day = datetime.date(start_date.year, start_date.month, 1).strftime('%Y-%m-%d')
last_day = datetime.date(start_date.year, start_date.month, num_days).strftime('%Y-%m-%d')
yield (first_day, last_day)
start_date += relativedelta(months = -1)
months_back = 5
print([i for i in get_last_months(datetime.datetime.today(), months_back)])
Output:
[('2020-10-01', '2020-10-31'), ('2020-09-01', '2020-09-30'), ('2020-08-01', '2020-08-31'), ('2020-07-01', '2020-07-31'), ('2020-06-01', '2020-06-30')]
This question already has answers here:
Iterating through a range of dates in Python
(25 answers)
Creating a range of dates in Python
(23 answers)
Closed 2 years ago.
This is an example of a URL, I want to use to scrape. The URL changes based on the date:
https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-01-01
I want to scrape data from the 1st of January to the 30th of June. How would I create a for loop to do this.
For example:
for day and month in URL:
request = requests.get(f'https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=2020-{month}-{day})
First set start date and end date. Then loop through them by increment by one day. then append the current date of iteration to the date parameter of your query:
import datetime
import requests
start_date = datetime.date(2019, 12, 31)
end_date = datetime.date(2020, 6, 30)
day_count = (end_date - start_date).days
date = start_date
for i in range(day_count):
date += datetime.timedelta(days=1)
request = requests.get('https://betfair-data-supplier-prod.herokuapp.com/api/daily_racing_results?date=' + str(date))
You can compare dates so increment by one day from the beginning of the time period
Import datetime as dt
delta = dt.timedelta(days=1)
i = dt.date(2020, 1, 1)
ed = dt.date(2020, 6, 30)
while i<=ed:
i += delta
print(f'this/day/{i}')
produces
this/day/2020-01-01
this/day/2020-07-02
this/day/2020-07-03
...
this/day/2020-06-29
this/day/2020-06-30
This question already has answers here:
How to add hours to current time in python
(4 answers)
Closed 3 years ago.
I want to get current time and add it with an integer of hours. Example now is 11.00pm, May 12, 2019. I want to add 3 hours more. So the result would be 2.00 am May 13, 2019. Please help me to datetime + hours(integer type)
import datetime
currentDT = datetime.datetime.now()
print('Now is: '+ str(currentDT))
hours = int(input()) #any hours you want
result = currentDT + hours #it will get the errors here
Use datetime.now to obtain the current time, and add a datetime.timedelta:
from datetime import datetime, timedelta
n_hours = 3
date = datetime.now() + timedelta(hours=n_hours)
print(datetime.now())
# 2019-05-12 19:16:51.651376
print(date)
# 2019-05-12 22:16:51.464890
This question already has answers here:
Days between two dates? [duplicate]
(4 answers)
Closed 5 years ago.
I need to design a code that will automatically generate the date X number of days from current date.
For that, I currently have a function that returns an epoch timestamp, which I then add the fixed number of days in seconds. However, I am currently stuck there and do not know how to convert the epoch timestamp into a Gregorian calendar date format (DD/MM/YYYY HH:MM). Displaying time is optional, can be rounded to the nearest day.
A way to do this without using an epoch timestamp and directly getting the current date in a readable format, printing it, and adding X days to it before generating the second date, is also fine, but I have no idea how that would work.
Any help/input would be much appreciated. Thanks in advance.
You can use timedelta.
import datetime as dt
x = 5 # Days offset.
now = dt.datetime.now()
>>> now + dt.timedelta(days=x)
datetime.datetime(2017, 12, 2, 21, 10, 19, 290884)
Or just using days:
today = dt.date.today()
>>> today + dt.timedelta(days=x)
datetime.date(2017, 12, 2)
Easy enough to convert back to a string using strftime:
>>> (today + dt.timedelta(days=x)).strftime('%Y-%m-%d')
'2017-12-02'
import datetime
x = 5
'''
Date_Time = datetime.datetime.now()#It wil give Date & time both
Time = datetime.datetime.now().time()#It will give Time Only
'''
Date = datetime.datetime.now().date()#It will give date only
print(Date + datetime.timedelta(days=x))#It will add days to current date
Output:
2017-12-03
This question already has answers here:
Generate a list of datetimes between an interval
(5 answers)
Closed 7 years ago.
How to generate a list of date objects for each day in a specific month and year.
I've tried using calendar module, but it's not what I want.
Using the timedelta module you can loop through all the possible values nicely and stop when the month changes.
from datetime import date, timedelta
def generate_month_days(year, month):
start_date = date(year, month, 01)
cur_date = start_date
while cur_date.month == start_date.month:
yield cur_date
cur_date += timedelta(days=1)
for d in generate_month_days(2015,11):
print d