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
Related
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 4 months ago.
i'm trying to get next month from selected date. if i using datetime.date.today() its ok. but i need working with request date. how can i solve it.Thanks
date = request.form['date']
print(date) // 2022-11-05
// nextmonth = datetime.date.today() + relativedelta.relativedelta(months=1)
nextmonth = date + relativedelta.relativedelta(months=1)
You have to convert str to date and then use relativedelta.relativedelta.
Not sure what format the date is, I am assuming it is YYYY-MM-DD.
You can change the day and month format as per what you want.
import datetime
from dateutil import relativedelta
date = '2022-11-05'
date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
nextmonth = date + relativedelta.relativedelta(months=1)
That should give you 2022-12-05.
This question already has answers here:
Convert String with month name to datetime
(2 answers)
Closed 6 months ago.
So I have a date in this format "August 10, 2022". I need to reformat the date in this way "2022-08-10". How do I do that in python ?.
For this, you can use datetime, specifically on this behaviour.
from datetime import datetime
a = "August 10, 2022"
b = datetime.strptime(a, '%B %d, %Y') # Converts to datetime format
c = b.strftime('%Y-%m-%d') # Converts from datetime to desired format
print(c)
# output
2022-08-10
This question already has answers here:
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Parse date string and change format
(10 answers)
Closed 2 years ago.
I have a string like this: Monday April 27 2020, 5:14pm
And I want to convert it to date time in python to become: 24-Apr-2020 5:14pm
Would python be able to do that?
from datetime import datetime
str_time = "Monday April 27 2020, 5:14pm"
formatted_time = datetime.strptime(a,"%A %B %d %Y, %I:%M%p")
new_str_representation = formatted_time.strftime("%d-%a-%Y %I:%M%p")
Converting string into datetime
https://docs.python.org/3/library/datetime.html
You can find more informations here.
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:
Convert UTC datetime string to local datetime
(16 answers)
Convert string "Jun 1 2005 1:33PM" into datetime
(26 answers)
Closed 9 years ago.
I get the client timestamp as Wed, 20 Nov 2013 13:53:35 GMT
I want to convert this into a python utc timestamp so that I can subtract it and get remaining number of days in month
def get_days_left_in_month(from_date):
# todo : how to validate if from_date is valid?
start_day, end_day = Day.get_start_end_days_for_year_and_month(
from_date.year, from_date.month
)
remaining_days = (end_day - from_date).days
return remaining_days if remaining_days == 0 else remaining_days - 1
How can I do this using python?
Try this
import time
a = "Wed, 20 Nov 2013 13:53:35 GMT"
time.strptime(a, "%a, %d %b %Y %H:%M:%S %Z")
And have a look here for available formats.