13

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
William
  • 2,621
  • 5
  • 32
  • 62

1 Answers1

19

You may use datetime.timedelta():

datetime.datetime.now() - datetime.timedelta(minutes=5)
Jan
  • 40,932
  • 8
  • 45
  • 77