Python 获取几天前的时间
最后更新于:2022-03-27 02:37:44
Python 获取几天前的时间
计算几天前并转换为指定格式。
实例 1
import time
import datetime # 先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() – datetime.timedelta(days = 3))
# 转换为时间戳
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
# 转换为其他字符串格式
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)
import datetime # 先获得时间数组格式的日期
threeDayAgo = (datetime.datetime.now() – datetime.timedelta(days = 3))
# 转换为时间戳
timeStamp = int(time.mktime(threeDayAgo.timetuple()))
# 转换为其他字符串格式
otherStyleTime = threeDayAgo.strftime("%Y-%m-%d %H:%M:%S")
print(otherStyleTime)
执行以上代码输出结果为:
2019-05-18 18:06:08
实例 2
import time
import datetime #给定时间戳
timeStamp = 1557502800
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray – datetime.timedelta(days = 3)
print(threeDayAgo)
import datetime #给定时间戳
timeStamp = 1557502800
dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
threeDayAgo = dateArray – datetime.timedelta(days = 3)
print(threeDayAgo)
执行以上代码输出结果为:
2019-05-07 15:40:00