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)
Related
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?
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:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parsing datetime in Python..?
(2 answers)
Closed 4 years ago.
I'm receiving data from the port.
This data has date and time.But not formatted.
Example:
'02012019', '090253'
I want to save it to the database and I want to convert this format.
02-01-2019 09:02:53
Is there a function for this? Or is it a problem if I write? It will run 200 times in 5 seconds.
Anybody have a suggestion?
from datetime import datetime
date_data ='02012019 09:02:53'
data = datetime.strptime(date_data,'%d%m%Y %H:%M:%S')
result = data.strftime('%d-%m-%Y %H:%M:%S')
# output '02-01-2019 09:02:53'
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:
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