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.
Related
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:
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
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
This question already has answers here:
Parse date string and change format
(10 answers)
Closed 8 years ago.
I have two dates:
Sat Mar 15 19:47:17 +0000 2014
2014-03-12 19:50:22.159411+00:00
I need to compare these two dates but I get the error
TypeError: can't compare datetime.datetime to unicode
How should I convert one of them?
The easiest method is to use the 3rd party dateutil lib, and do:
from dateutil.parser import parse as parse_date
unicode_text = 'Sat Mar 15 19:47:17 +0000 2014'
dt = parse_date(unicode_text)
# 2014-03-15 19:47:17+00:00
if dt == other_datetime:
# do something
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.