Easy Tutorial
❮ Ref Math Acos Python File Truncate ❯

Python Get Time a Few Days Ago

Python3 Examples

Calculate the time a few days ago and convert it to a specified format.

Example 1

import time
import datetime

# Get the date in the form of a time array
threeDayAgo = (datetime.datetime.now() - datetime.timedelta(days = 3))
# Convert to timestamp
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
# Convert to other string formats
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)

Executing the above code outputs:

2019-05-18 18:06:08

Example 2

import time
import datetime

# Given timestamp
timeStamp = 1557502800
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray - datetime.timedelta(days = 3)
print(threeDayAgo)

Executing the above code outputs:

2019-05-07 15:40:00

Python3 Examples

❮ Ref Math Acos Python File Truncate ❯