How to get previous month from a str datetime in Python? [duplicate] - python

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)

Related

how to calculate date after 8 months from delivery_date? [duplicate]

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?

How to get date in python? [duplicate]

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)

How to print the next 3 dates from today? [duplicate]

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)

Obtain the datetime object for the previous day at midnight [duplicate]

This question already has answers here:
What was midnight yesterday as an epoch time?
(7 answers)
Closed 5 years ago.
I have a datetime object in Python, for, let's say, 25/06/2017 11:22:33. I'd like to find a pythonic way of getting a datetime object from that one that would represent 24/06/2017 00:00:00.
I can think of:
day_before = now - datetime.timedelta(days=1,
hours=now.hour,
minutes=now.minute,
seconds=now.second)
But I was wondering if there is a more concise way.
from datetime import date, timedelta
yesterday = date.today() - timedelta(1)
From here.

Add 1 day to my date in Python [duplicate]

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'

Categories

Resources