16

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
  • 3,724
  • 9
  • 43
  • 76

1 Answers1

27

You may use datetime.timedelta():

datetime.datetime.now() - datetime.timedelta(minutes=5)
Jan
  • 42,290
  • 8
  • 54
  • 79
  • 2
    Just a note that you may avoid repeating datetime.datetime by importing as follows: `from datetime import datetime, timedelta` – MediaVince Sep 20 '22 at 17:56