import time import datetime today = datetime.datetime.now() # delta = datetime.timedelta(hours=1) delta = datetime.timedelta(days=7) start = today - delta dt = start.strftime("%Y-%m-%d %H:%M:%S") timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S") startts = int(time.mktime(timeArray)) * 1000000 print(startts)
另一种方法
import pytz import time import datetime tz = pytz.timezone('Asia/Shanghai') now = datetime.datetime.now(tz) sevendays = timedelta(days=8) start = now - sevendays start2 = datetime.datetime(start.year, start.month, start.day) startts = int(time.mktime(start2.timetuple())*1000) print(startts)
如果大神们有更好的写法,还望不吝赐教。