How do I change a date to YYYYMMDD in Python? [duplicate] - python

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.

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 convert current date to a DD/MM/YYYY [duplicate]

This question already has answers here:
How to print a date in a regular format?
(25 answers)
Closed 2 years ago.
How can I get the the import function to convert the date into a European date ? Example: (‘4/7/14’) returns ‘07/04/12’
import datetime
today = datetime.date.today()
print(today)
that is what datetime.strftime is for. have a look at the format codes to get your desired result.
for example:
import datetime
today = datetime.date.today()
print(today.strftime("%Y-%m-%d")) # 2020-12-02
print(today.strftime("%a %d %b %Y")) # Wed 02 Dec 2020
print(today.strftime("%d/%m/%Y")) # 02/12/2020

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)

Parse this string "20160713161212" to datetime [duplicate]

This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 6 years ago.
I have datetime a string "20160713161212".
It is a datetime in string with format YYYYMMDDHHIISS
How to parse it to string with format "07/13/2016 16:12:12" in Python?
I think that's what you are looking for:
from datetime import datetime
dt = datetime.strptime('20160713161212', '%Y%m%d%H%M%S')
new_dt = dt.strftime("%Y/%m/%d %H:%M:%S")
For further info you can take a look 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