def getRandDate(self,flag):
'''用于生成随机的日期,如果flag小于零则生成小于今天的日期,否则生成大于今天日期的日期'''
if flag == '0':
a1 = (1990, 1, 1, 0, 0, 0, 0, 0, 0) # 设置开始日期时间元组(1976-01-01 00:00:00)
a2 = (2016, 12, 31, 23, 59, 59, 0, 0, 0) # 设置结束日期时间元组(1990-12-31 23:59:59)
start = time.mktime(a1) # 生成开始时间戳
end = time.mktime(a2) # 生成结束时间戳
# 随机生成10个日期字符串
t = random.randint(start, end) # 在开始和结束时间戳中随机取出一个
date_touple = time.localtime(t) # 将时间戳生成时间元组
date = time.strftime("%Y%m%d", date_touple) # 将时间元组转成格式化字符串(1976-05-21)
elif flag == '1':
a1 = (2010, 1, 1, 0, 0, 0, 0, 0, 0) # 设置开始日期时间元组(1976-01-01 00:00:00)
a2 = (3000, 12, 31, 23, 59, 59, 0, 0, 0) # 设置结束日期时间元组(1990-12-31 23:59:59)
start = time.mktime(a1) # 生成开始时间戳
end = time.mktime(a2) # 生成结束时间戳
# 随机生成10个日期字符串
t = random.randint(start, end) # 在开始和结束时间戳中随机取出一个
date_touple = time.localtime(t) # 将时间戳生成时间元组
date = time.strftime("%Y%m%d", date_touple) # 将时间元组转成格式化字符串(197605-1)
return date
python自动生成指定格式的日期串date = time.strftime("%Y%m%d", date_touple)
最新推荐文章于 2024-04-14 15:30:00 发布