This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 5 years ago.
I have weird date format in logs: 2017-01-24T23:52:14
I am trying to convert this string into datetime Python object like this:
date_from_log = datetime.strptime('2017-01-24T23:52:14', '%Y-%m-%dT%I:%M:%S')
but I get:
ValueError: time data ' 2017-01-24T23:25:39 ' does not match format '%Y-%m-%dT%I:%M:%S'
What is wrong?
You should be using %H for hours!!
date_from_log = datetime.strptime(s, '%Y-%m-%dT%H:%M:%S')
Output:
datetime(2017, 1, 24, 23, 52, 14)
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:
Converting unix timestamp string to readable date
(19 answers)
Closed 1 year ago.
I have a very silly question... how I can convert this timestring to a normal datetime?
20170509.54166667
Greetings.
You can do:
import datetime
datetime.datetime.fromtimestamp(20170509.54166667)
# which returns this:
datetime.datetime(1970, 8, 22, 11, 55, 9, 541667)
This question already has answers here:
How to truncate the time on a datetime object?
(18 answers)
How to display locale sensitive time format without seconds in python
(4 answers)
Closed 4 years ago.
I have a python datetime object that I want to display on a website, however the time shows in the format hh:mm:ss and I want to display it in the format hh:mm.
I have tried using the replace method as per the following:
message.timestamp.replace(second='0', microsecond=0)
However this doesn't get red of the seconds it just replaces it with hh:mm:00
Use strftime() function
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 12, 27, 11, 14, 37, 137010)
>>> now = datetime.now()
>>> now.strftime("%H:%M")
'11:15'
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:
How can I account for period (AM/PM) using strftime?
(5 answers)
Closed 7 years ago.
I'm trying to convert a string date to epoch, but it doesn't seem to pick up the am or pm. The time always defaults to am.
I've tried this:
dt = '2015-05-04 5:55PM'
pattern = '%Y-%m-%d %H:%M%p'
epochDate = int(time.mktime(time.strptime(dt, pattern)))
print epochDate
# Result
1430684700
# Checking output
datetime.datetime.fromtimestamp(1430684700).strftime('%Y-%m-%d %H:%M:%S%p')
# Doesn't show PM
'2015-05-04 05:55:00AM'
I'm not sure what I've done wrong here?
You should use the python-dateutil library:
>>> import dateutil.parser
>>> dateutil.parser.parse('2015-05-04 5:55PM')
datetime.datetime(2015, 5, 4, 17, 55)
>>> dateutil.parser.parse('2015-05-04 5:55AM')
datetime.datetime(2015, 5, 4, 5, 55)