Python Get Time a Few Days Ago
Calculate a few days ago and convert to the specified format.
Example 1
import time
import datetime
# First get the date in time array format
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:00Other Extensions