This question already has answers here:
How to create a DateTime equal to 15 minutes ago?
(9 answers)
Closed 2 years ago.
I'm using python to get time of 5 minutes ago.
Code:
import datetime
import time
now = datetime.datetime.now()
current_time = now.strftime(f"%Y-%m-%d %H:%M")
print(current_time)
2020-07-27 08:35:00
My question is how to get the time of 5 minutes ago.
something like
current_time-5mins
You may use datetime.timedelta():
datetime.datetime.now() - datetime.timedelta(minutes=5)
Related
This question already has answers here:
How to calculate number of days between two given dates
(15 answers)
Closed 2 months ago.
How can I get the number of days since 1/1/1972 with CustomerSince (a data frame) below:
CustomerSince
1 2011-07-24 21:27:16.617
2 2011-10-05 21:27:16.617
3 2012-07-24 21:27:16.617
4 2010-08-31 21:27:16.617
5 2011-07-24 21:27:16.617
Thanks for the reply.
If your question is the difference between two dates, This solution may help you:
from datetime import datetime
str_dt1 = '1972/01/01'
str_dt2 = '2011/07/24'
dt1 = datetime.strptime(str_dt1, "%Y/%m/%d")
dt2 = datetime.strptime(str_dt2, "%Y/%m/%d")
delta = dt2 - dt1
print(f'Difference is {delta.days} days') # Difference is 14449 days
This question already has answers here:
How do I find the time difference between two datetime objects in python?
(21 answers)
Closed 3 years ago.
When I execute ll command I get the timestamps:
-rw-rw-r--+ 1 4167 May 5 17:19 file A
-rw-rw-r--+ 1 2721 May 4 17:08 file B
I want the difference between timestamps of A and B
I tried this:
datetime.fromtimestamp(getmtime(file)).strftime('%h %m %s'))
It gives
May 05 1557032395
May 04 1557084082
Please help me get the time difference
Looks like you need.
import os
import datetime
print(datetime.datetime.fromtimestamp(os.path.getmtime("file A")) - datetime.datetime.fromtimestamp(os.path.getmtime("file A")))
You can subtract 2 datetime objects to get the difference.
This question already has an answer here:
Python datetime add
(1 answer)
Closed 4 years ago.
I have a string variable some_time = "12:30", and I want to add 2 hours to it so the result is "14:30".
I believe by first turning the string into a timeformat,
temp_time = datetime.datetime.strptime(thetime, '%H:%M').time()
so that
>>>print (temp_time)
12:30:00
And then use Pytz can solve it. But I can't seem to find a Pytz command that deals with hh:mm alone, without yy:dd:mm
Any solutions?
If you use a timedelta, you can add two hours like:
Code:
def add_two_hours(time_string):
the_time = dt.datetime.strptime(time_string, '%H:%M')
new_time = the_time + dt.timedelta(hours=2)
return new_time.strftime('%H:%M')
Test Code:
import datetime as dt
print(add_two_hours('12:30'))
print(add_two_hours('23:30'))
Results:
14:30
01:30
This question already has answers here:
How can I parse a time string containing milliseconds in it with python?
(7 answers)
Closed 7 years ago.
How to convert "2013-10-21 12:00:00.004" to datetime object in Python?
The problem is there is decimal number in seconds.
here is a working example
import datetime
dt = datetime.datetime.strptime("2013-10-21 12:00:00.004", "%Y-%m-%d %H:%M:%S.%f")
print (dt.microsecond)
This question already has answers here:
How to subtract a day from a date?
(8 answers)
Closed 8 years ago.
To get now, I can do:
now = datetime.datetime.now()
How would I get 24 hours ago?
now - 24 hrs. ?
Use timedelta:
datetime.datetime.now() - datetime.timedelta(days=1)
http://docs.python.org/2/library/datetime.html#timedelta-objects
you could use:
datetime.date.fromordinal(datetime.date.today().toordinal()-1)
Get yesterday's date in Python, DST-safe
this will help with formatting:
http://www.cyberciti.biz/faq/howto-get-current-date-time-in-python/