Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the time of 5 minutes ago in python [duplicate]

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
like image 856
William Avatar asked Jul 28 '20 05:07

William


1 Answers

You may use datetime.timedelta():

datetime.datetime.now() - datetime.timedelta(minutes=5)
like image 120
Jan Avatar answered Sep 17 '22 17:09

Jan