This question already has answers here:
How do I calculate the date six months from the current date using the datetime Python module?
(47 answers)
Closed 1 year ago.
I want to make a function that returns the expiration date.
this is the field to calculate from?
delivery_date = models.DateTimeField(auto_now_add=True)
this is how to do it in python:
from datetime import date
from dateutil.relativedelta import relativedelta
date_of_interest = date.today() # get this from your app
eight_months = date_of_interest + relativedelta(months=+8)
print(date_of_interest)
print(eight_months)
from:
How do I calculate the date six months from the current date using the datetime Python module?
Related
This question already has answers here:
python date of the previous month
(18 answers)
Finding the previous month
(5 answers)
Closed 2 years ago.
I have a str variable which is my_variable = str(201705).
I want to get the previous month of my_variable using datetime in python. Is there a method to accomplish this? So far I have:
from datetime import datetime
my_variable = str(201705)
month = datetime.strptime(my_variable, '%Y%m').strftime("%b%Y")
This gives me my current variables month in str format, so May2017. I want to get just the previous month stored in a new variable. So my_new_month would = April.
Is this possible using the python datetime library, and if not, is there another solution to accomplish this?
from datetime import datetime, timedelta
my_variable = str(202102)
d1 = datetime.strptime(my_variable, '%Y%m')
days = d1.day
# use timedelta to subtract n+1 days from current datetime object
d2 = d1 - timedelta(days=days+1)
# get month of d2
print(d2.month)
This question already has answers here:
How do I convert a datetime to date?
(10 answers)
Closed 2 years ago.
I am Trying to get the current date
This is my code
import datetime
x = datetime.datetime.now()
print(x)
But I need Only Date not date and Time both
import datetime
x = datetime.datetime.now().strftime('%Y-%m-%d')
print(x)
This question already has answers here:
Getting today's date in YYYY-MM-DD in Python?
(12 answers)
Closed 6 years ago.
I have the below code which works out the date 6 months ago from todays date.
import datetime
from dateutil.relativedelta import relativedelta
from datetime import date
six_months = date.today() + relativedelta(months=-6)
However I would like six_months to be in "%Y%m%d" format.
Thank you for your help.
You're looking for strftime.
six_months.strftime('%Y%m%d')
This should do the job for you.
This question already has answers here:
Adding days to a date in Python
(16 answers)
Closed 2 years ago.
I have the following date format:
year/month/day
In my task, I have to add only 1 day to this date. For example:
date = '2004/03/30'
function(date)
>'2004/03/31'
How can I do this?
You need the datetime module from the standard library. Load the date string via strptime(), use timedelta to add a day, then use strftime() to dump the date back to a string:
>>> from datetime import datetime, timedelta
>>> s = '2004/03/30'
>>> date = datetime.strptime(s, "%Y/%m/%d")
>>> modified_date = date + timedelta(days=1)
>>> datetime.strftime(modified_date, "%Y/%m/%d")
'2004/03/31'
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