How to add 6 months to a series of datetime in Python? - python

If I have a series of date such as "2020-01-01,2020-01-02,...." Then how can I add 6 months to every date? The result should be "2020-07-01,2020-07-02,.....". In Excel I can use Edate function so is there any similar function that also works in python?

You can do the following:
from dateutil.relativedelta import relativedelta
for i, date in enumerate(datelist):
datelist[i] = date + relativedelta(months=+6)
datelist refers to your series with the dates

Related

how to get last month days from a period of datetime in python?

Hi I have a startDate and endDate in python, the goal here is to get the same day but previous month and if the endDate does not exist returns back last date of the month.
From this
startDate = '01-03-2022'
endDate= '31-03-2022'
to
prevStartDate ='01-02-2022'
prevEndDate = '28-02-2022'
where
from datetime import datetime
from dateutil.relativedelta import relativedelta
period = endDate-startDate
prevStartDate = startDate- relativedelta(months=1)
prevEndDate = endDate+ period
how to do eosmonth like in excel so that i can put the if condition for prevEndDate the last month? or if there's another way to approach this?

How do I take end date as 11 months ahead of start date in the datetime format in Python?

I am taking start date as user input in the datetime format in python. Using the start date, I want to take end date as 11 months from start date.
start_date= month_dt
end_date =
Here, month_dt is taken in taken as user input in the datetime format, e.g. 2020-01-01 . How do I take end_date as 11 months ahead of the start date?
import datetime
from dateutil.relativedelta import relativedelta
print(datetime.date.today() - relativedelta(months=+11))
Current date reduced by 11 months

How to get last three months in year-mon format in python?

I want to get last three months from todays date in year-mon format. For example if todays date is 2021-08-04 then I want list of last three months as -
["2021-05", "2021-06", "2021-07"]
I have no idea how to start with this. Any help will be appreciated.
use dateutil's relativedelta to get consistent results, as not all months have equal number of days. E.g.
from datetime import datetime
from dateutil.relativedelta import relativedelta
NOW = datetime.now() # reference date
delta = relativedelta(months=-1) # delta in time
n = 3 # how many steps
fmt = lambda dt: dt.strftime("%Y-%m") # formatter; datetime object to string
l = sorted((fmt(NOW+delta*i) for i in range(1, n+1)))
# l
# ['2021-05', '2021-06', '2021-07']

Comparing date in python and applying a condition on comparison [duplicate]

This question already has answers here:
How to compare two dates?
(6 answers)
Closed 3 years ago.
i want to compare/subtract two date's due date from current date in python and apply a condition on this subtraction that if the difference is >0 days then calculate fine by multiplying difference with fine per day
from datetime import date
a=date.today()
issuedate=date(2019,5,9)
duedate#should be 5 days after issue date i can't find the method for doing this
check=a-duedate
# if check>0days:
# print(check days*40)
You need to use the timedelta function in the datetime module:
from datetime import date
import datetime
a=date.today()
issuedate=date(2019,5,9)
duedate = issuedate+datetime.timedelta(days=5)
check=a-duedate
print(check>=datetime.timedelta(days=0))
Use timedelta
from datetime import timedelta
duedate = issuedate + timedelta(days=5)
check=(a-duedate).days

Creating a list of past 5 dates from given date in string format in Python

I have a string which contains a date. I need to create a list with previous 5 dates including the given date in python.
Example:
date = ['09-26-2016']
dates = ['09-21-2016','09-22-2016','09-23-2016','09-24-2016','09-25-2016','09-26-2016']
I need to create dates given date as input. The input is in string format.
You need to use datetime to parse the strings into dates and calculate timedelta
from datetime import datetime, timedelta
d='09-24-2016'
d1=datetime.strptime(d, '%m-%d-%Y')
[(d1-timedelta(days=i)).strftime('%m-%d-%Y') for i in range(6,0,-1)]
['09-20-2016','09-21-2016', '09-22-2016', '09-23-2016', '09-24-2016', '09-25-2016']
from datetime import datetime, timedelta
date = ['09-26-2016']
date_time_obj = datetime.strptime(date[0], '%m-%d-%Y')
dates=[]
dates.append(date_time_obj)
for i in range(0,4):
print(i)
j=i+1
print(j)
dates_list= date_time_obj.date() - timedelta(days=j)
dates.append(dates_list)
print(dates)

Categories