基本都是固定写法,套参数就行
''' 获取当前时间前一天的随机时间 参考文档:https://www.cnblogs.com/HugJun/p/11660352.html 注意: 时间戳的单位是秒 ''' import time, datetime, random # 1、获取今天的日期 today = datetime.date.today() print('今天的日期:', today) # 2、获取昨天的日期 yesterday = today - datetime.timedelta(days=1) print('昨天的日期:', yesterday) # 3、获取昨天的开始时间戳 yesterday_start_time = int(time.mktime(time.strptime(str(yesterday), '%Y-%m-%d'))) print('昨天的开始时间戳:', yesterday_start_time) # 4、获取昨天的结束时间戳 yesterday_end_time = int(time.mktime(time.strptime(str(today), '%Y-%m-%d'))) - 1 print('昨天的结束时间戳:', yesterday_end_time) # 5、获取随机时间 def random_time(yesterday_start_time, yesterday_end_time): random_time = random.randrange(yesterday_start_time, yesterday_end_time) print('昨天内的随机时间戳:', random_time) jia_time = time.localtime(random_time) really_time = time.strftime('%Y-%m-%d %H:%M:%S', jia_time) print('昨天内的随机时间:', really_time) random_time(yesterday_start_time, yesterday_end_time)
参考链接:
https://www.cnblogs.com/HugJun/p/11660352.html
结束!!