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)
Related
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 do I calculate the date six months from the current date using the datetime Python module?
(47 answers)
Closed 3 years ago.
How do I remove 3 months from the date? tried to use relativedelta, but I don't have the lib, so is there another way?
from datetime import datetime
now = datetime.now()
print("timestamp =", now)
You can use:
import datetime
a = datetime.datetime.now()
b = a - datetime.timedelta(weeks=12)
Note that you cannot give months as inputs, but weeks and days, and smaller
Perhaps, subtracting with datetime.timedelta can help you estimate the date:
import datetime
datetime.datetime(2019,5,31) - datetime.timedelta(3*365.25/12)
This result is not completely accurate, as it does not account for the leap years in the usual way, however for less precise calculations within a year should suffice. If you need accuracy you will need to use the method below.
For dateutil.relativedelta, you need to first install the module with pip install python-dateutil, and then use it in the following way:
import datetime
from dateutil.relativedelta import relativedelta
datetime.datetime(2019,5,31) - relativedelta(months=3)
If you want to perform complex operations on datetime objects, I can only recommend you to use the library arrow that will handle all the edge cases for you.
The documentation of this library is available here: https://arrow.readthedocs.io/en/latest/
For instance in your case:
>>> import arrow
>>> a = arrow.utcnow()
>>> a
<Arrow [2019-11-20T13:03:52.518238+00:00]>
>>> a.datetime
datetime.datetime(2019, 11, 20, 13, 3, 52, 518238, tzinfo=tzutc())
>>> b = a.shift(months=-3)
>>> b
<Arrow [2019-08-20T13:03:52.518238+00:00]>
>>> b.datetime
datetime.datetime(2019, 8, 20, 13, 3, 52, 518238, tzinfo=tzutc())
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:
Stripping off the seconds in datetime python
(5 answers)
Closed 4 years ago.
When i preform datetime.now() to get the current time, it gets printed (with print) like this:
2018-06-14 13:21:02.331933
I want to ignore seconds and microseconds and to have it printed out like this:
2018-06-14 13:21:00
I know that I can convert to a string of my choosing with strftime but i'm not looking to alter the string but to change the datetime object itself so that it has 0 seconds and 0 milliseconds (and it seems stupid to do strftime and immediately strptime to convert back).
In other words to convert from datetime(2018, 6, 14, 14, 7, 27, 326853) to datetime(2018, 6, 14, 14, 7)
I've tried like this:
now = datetime.now()
now = now - timedelta(seconds=now.second, microseconds=now.microsecond)
And that works, but i'm sure there's a simpler and more efficient way (I need to do this thousands of times when iterating through different files). So my question is if i'm right that there is a simpler way and if so what are your solutions to such a problem?
Use datetime.replace:
now = datetime.now().replace(second=0, microsecond=0)
Use the datetime.replace() method to create a new instance, with specific attributes set to your desired value:
now = datetime.now().replace(second=0, microsecond=0)
Demo:
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2018, 6, 14, 12, 16, 4, 738362)
>>> datetime.now().replace(second=0, microsecond=0)
datetime.datetime(2018, 6, 14, 12, 16)
This question already has answers here:
How do I parse an ISO 8601-formatted date?
(29 answers)
Closed 8 years ago.
I am getting
time data '2015-02-10T13:00:00Z' does not match format '%Y-%m-%d %H:%M:%S'
I tried:
import datetime
datetime.datetime.strptime('2015-02-10T13:00:00Z', '%Y-%m-%d %H:%M:%S')
and
import time
time.strptime('2015-02-10T13:00:00Z', '%Y-%m-%d %H:%M:%S')
what am I doing wrong?
As a quick workaround, you could add T and Z characters into the datetime formatting:
import datetime # v note v
datetime.datetime.strptime('2015-02-10T13:00:00Z', '%Y-%m-%dT%H:%M:%SZ')
# datetime.datetime(2015, 2, 10, 13, 0) # ^ note ^
But it's better to use something that is able to parse ISO-formatted date & time. For example, dateutil.parser:
import dateutil.parser
dateutil.parser.parse('2015-02-10T13:00:00Z')
# datetime.datetime(2015, 2, 10, 13, 0, tzinfo=tzutc())