2

I would like to have a python3 script which gets the current time - 10 minutes and it converts it to epoch time in milliseconds

The problem is that I have no idea how to start.

something witch in bash looks like this:

$(( $(date -d '-10 minutes' +%s%N)/1000000 )))
zozo6015
  • 557
  • 2
  • 11
  • 27
  • 1
    [(1)](https://stackoverflow.com/questions/415511/how-to-get-current-time-in-python) [(2)](https://stackoverflow.com/questions/42594164/subtract-time-from-datetime-time-object) [(3)](https://stackoverflow.com/questions/6999726/how-can-i-convert-a-datetime-object-to-milliseconds-since-epoch-unix-time-in-p) – glibdud Dec 08 '17 at 13:32

1 Answers1

4

You can use datetime.datetime and datetime.timedelta to achieve that:

import datetime

current_time = datetime.datetime.now()  # use datetime.datetime.utcnow() for UTC time
ten_minutes_ago = current_time - datetime.timedelta(minutes=10)

ten_minutes_ago_epoch_ts = int(ten_minutes_ago.timestamp() * 1000)  # in miliseconds
zwer
  • 24,943
  • 3
  • 48
  • 66